-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Optimize thread pool #14259
Optimize thread pool #14259
Conversation
… optimize-thread-pool test=develop
test=develop
e883305
to
4062f00
Compare
May be use thread pool under thirdparty is OK. |
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.
Maybe move on to thirdparty implementation later.
what's the gain of this optimization? |
tasks_.push(std::move(task)); | ||
{ | ||
std::unique_lock<std::mutex> lock(mutex_); | ||
if (!running_) { |
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.
Is this running_
check needed?
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 we need it, but it can work if not add.
@@ -57,10 +57,10 @@ ThreadPool::ThreadPool(int num_threads) : running_(true) { | |||
ThreadPool::~ThreadPool() { | |||
{ | |||
// notify all threads to stop running | |||
std::lock_guard<std::mutex> l(mutex_); | |||
std::unique_lock<std::mutex> l(mutex_); |
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.
is unique_lock faster?
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.
No, here just want to be consistent with other place.
} | ||
scheduled_.notify_all(); |
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.
some doc said notify_all doesn't need lock. But in reality, I see lots of projects locking this
refs:
https://stackoverflow.com/questions/44387584/double-free-or-corruption-prev-error-when-working-with-threads-and-mutex
https://stackoverflow.com/questions/17101922/do-i-have-to-acquire-lock-before-calling-condition-variable-notify-one
https://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html