October 2013
Intermediate to advanced
368 pages
9h 20m
English
We want to demonstrate that the worker thread can pull and execute multiple work items from the queue.
| c9/7/ThreadPoolTest.cpp | |
| | TEST(AThreadPool, ExecutesAllWork) { |
| | pool.start(); |
| | unsigned int count{0}; |
| | unsigned int NumberOfWorkItems{3}; |
| | condition_variable wasExecuted; |
| | Work work{[&] { |
| | std::unique_lock<std::mutex> lock(m); |
| | ++count; |
| | wasExecuted.notify_all(); |
| | }}; |
| | for (unsigned int i{0}; i < NumberOfWorkItems; i++) |
| | pool.add(work); |
| | unique_lock<mutex> lock(m); |
| | CHECK_TRUE(wasExecuted.wait_for(lock, chrono::milliseconds(100), |
| | [&] { return count == NumberOfWorkItems; })); |
| | } |
Our implementation introduces a while loop and a boolean flag that tells the loop to stop when the ThreadPool ...