Replaced user-configured sleep time with condition variables #23
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.
Describe the changes
I thought it would be useful to do away with the arbitrary sleeps in this library. I've replaced the sleeps with
std::condition_variable
s.task_available_condition_variable
) to be notified once work is added.wait_for_tasks
, the thread will wait on a different condition variable (tasks_done_condition_variable
). Each time a worker thread finishes some work, it will notify on this condition variable, possibly waking upwait_for_tasks
if all of the work is actually completed.parallelize_loop
, yet another condition variable (loop_done_condition_variable
; this time, a local variable) is waited on to synchronize the blocks of the loop. Once a block of the loop is completed, if it was the last block, it will notify the condition variable.paused
as a public data member of the class posed an issue with the condition variables. If the user un-pauses execution, the pool should wake up all worker threads. For this reason, I madepaused
private and provided public functions to modify the value.Previously,
tasks
was protected by anstd::mutex
(now namedtasks_mutex
) andtasks_total
was anstd::atomic
. Sincetasks_total
is tested under the condition variable, it made sense to make it no longer atomic and to move it under the protection of thetasks_mutex
.I adapted thead_pool_test.cpp for the changes to the interface.
The file thead_pool_test.cpp used carriage returns and linefeeds for line endings, while thread_pool.hpp only used linefeeds. I removed the carriage returns from thread_pool_test.cpp to make them consistent.
Testing
Have you tested the new code using the provided automated test program
thread_pool_test.cpp
and/or performed any other tests to ensure that it works correctly? If so, please provide information about the test system(s):Yes, I built thead_pool_test.cpp and ran it hundreds of times (to hopefully cover some different possible thread-execution-order possibilities) built by two different compilers:
g++
version 8.4.0, flags-std=c++17 -pthread -O3
MSVC 14.29
, with flag\std:c++17
in default debug mode (\Od
)MSVC 14.29
, with flag\std:c++17
in default release mode (\O2
)I also created my own little load testing tool which creates a bunch of tasks (which just sleep) with random delays.
Machine details: