My collection of Concurrency related header-only classes while going through C++ Concurrency in Action
book.
Following is the brief description of classes
A thread pool to manage a group of worker threads to execute tasks - It expects a template parameter for the underlying container to store tasks:
- By default, it uses
BlockingQueue
to store and extract tasks - Otherwise,
PriorityQueue
is another candidate for underlying queue
Threadpool
permits concurrent invocation of add_task
member methods and other helper methods
- except
stop
andstop_early
- and methods which permit concurrent invocation have been annotated with
thread_safe
A std::condition_variable
based BlockingQueue
to hold tasks in a queue and supports following methods:
push
to push an item (task) on the queuepop
waits forstd::chrono::milliseconds::max()
for an element to be on queue to pop, otherwise throwsstd::runtime_error
try_pop_for
waits for a user specified time interval for an element to be on queue and returns a pair:pair.first
is astd::unique_ptr
containing the item, otherwise nullptr if there was nothing in the queuepair.second
returns true if queue has been closed, otherwise false
try_pop
liketry_pop_for
, only difference is that it tries for_wait_time
milliseconds (set in constructor)
Following is the class template details
tparam T
: type of tasks, can be either executables or executables wrapped insidePriorityWrapper
to add prioritytparam Container
:std::queue<T>
orstd::multiset<T>
, not any other container. This is enforced in constructorstd::multiset<T>
is supported in case item has a priority (e.g. wrapped insidePriorityWrapper
)
As the classes are header-only, user can simply copy the include directory and use in their projects.