-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional variable instead of sleep_for #23
Open
yvoinov
wants to merge
1
commit into
inkooboo:master
Choose a base branch
from
yvoinov:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- worker.hpp Thu Mar 22 23:20:12 2018 | ||
+++ worker.hpp Sat Mar 24 22:28:13 2018 | ||
@@ -2,6 +2,8 @@ | ||
|
||
#include <atomic> | ||
#include <thread> | ||
+#include <condition_variable> | ||
+#include <mutex> | ||
|
||
namespace tp | ||
{ | ||
@@ -78,6 +80,8 @@ | ||
Queue<Task> m_queue; | ||
std::atomic<bool> m_running_flag; | ||
std::thread m_thread; | ||
+ std::mutex m_conditional_mutex; | ||
+ std::condition_variable m_conditional_lock; | ||
}; | ||
|
||
|
||
@@ -121,6 +125,7 @@ | ||
inline void Worker<Task, Queue>::stop() | ||
{ | ||
m_running_flag.store(false, std::memory_order_relaxed); | ||
+ m_conditional_lock.notify_all(); | ||
m_thread.join(); | ||
} | ||
|
||
@@ -140,6 +145,7 @@ | ||
template <typename Handler> | ||
inline bool Worker<Task, Queue>::post(Handler&& handler) | ||
{ | ||
+ m_conditional_lock.notify_one(); | ||
return m_queue.push(std::forward<Handler>(handler)); | ||
} | ||
|
||
@@ -171,7 +177,8 @@ | ||
} | ||
else | ||
{ | ||
- std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
+ std::unique_lock<std::mutex> lock(m_conditional_mutex); | ||
+ m_conditional_lock.wait(lock); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should do the notification after you have added the job to the queue otherwise the worker might try to pull the job too soon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мммммммм, in theory may be. In practice....hardly. Also, how you imagine such implementation? I see only one way - additional local variable requires, so more assembler, slower execution... No, not so good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're going to worry about the cost of that it might be more interesting to look at the call to
notify_one
you have for every posted job. Compared to that cost saving the bool result is essentially free.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take a look on my own fork of this library. Done long time ago. :) This PR is years old :)
Anyway, you can make you own fork, perform benchmark and compare. :)