问题
Suppose, we have a typical queue consumer loop with a stop flag:
class consumer {
atomic<bool> keep_running;
concurrent_bounded_queue queue;
void loop() {
while (keep_running)
// <- danger zone
queue.pop();
}
void stop() {
keep_running = false;
queue.abort();
}
};
If used as written, stop will fail when called after the keep_running check but before the pop call since abort only terminates pop calls already in progress.
We also can't use a mutex to make sure the check and the pop are performed atomically, since then we would not be able to call abort while pop is in progress.
What is the correct usage here?
来源:https://stackoverflow.com/questions/47182231/how-to-use-tbbs-concurrent-bounded-concurrent-queue-abort-correctly