Another excellent video from Herb Sutter, this time on C++ Concurrency. The Monitor class is based on his Wrapper pattern
and allows the caller to group related calls into a transaction, with synchronisation supplied via a mutex.
The Concurrent class replaces the mutex with a concurrent_queue to avoid the caller blocking whilst the tasks complete.
What’s especially elegant is ~Concurrent which pushes a done function onto message queue in order to signal not to wait for any more tasks (the concurrent_queue.pop() is assumed to wait until the next task is pushed). The done data member is not required to be atomic because it is only mutated on the worker thread, never on the calling thread.
Pingback: Concurrency with C++11 | musingstudio