What is the best way to wait on multiple condition variables in C++11?

前端 未结 3 619
夕颜
夕颜 2020-12-08 19:46

First a little context: I\'m in the process of learning about threading in C++11 and for this purpose, I\'m trying to build a small actor class

3条回答
  •  无人及你
    2020-12-08 20:04

    Maybe this can works:

    get rid of interrupt.

     message wait_and_pop(std::condition_variable& interrupt) {
        std::unique_lock lock(mutex);
        {
            new_msg_notification.wait(lock,[&]{
                return !queue.empty() || stop;
            });
    
            if( !stop )
            {
                auto msg(std::move(queue.front()));
                queue.pop();
                return msg;
            }
            else
            {
                return NULL; //or some 'terminate' message
            }
    }
    

    In destructor, replace interrupt.notify_all() with new_msg_notification.notify_all()

提交回复
热议问题