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
BlockingQueueto store and extract tasks - Otherwise,
PriorityQueueis another candidate for underlying queue
Threadpool permits concurrent invocation of add_task member methods and other helper methods
- except
stopandstop_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:
pushto push an item (task) on the queuepopwaits forstd::chrono::milliseconds::max()for an element to be on queue to pop, otherwise throwsstd::runtime_errortry_pop_forwaits for a user specified time interval for an element to be on queue and returns a pair:pair.firstis astd::unique_ptrcontaining the item, otherwise nullptr if there was nothing in the queuepair.secondreturns true if queue has been closed, otherwise false
try_popliketry_pop_for, only difference is that it tries for_wait_timemilliseconds (set in constructor)
Following is the class template details
tparam T: type of tasks, can be either executables or executables wrapped insidePriorityWrapperto 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.