How to use TBB's concurrent_bounded_concurrent_queue abort correctly?

北城余情 提交于 2019-12-11 15:24:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!