Exposing Concurrency Issues

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 ...

Get Modern C++ Programming with Test-Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.