小编sti*_*ima的帖子

c ++ 11在信号处理程序中使用条件变量

std::condition_variable::notify_one在信号处理程序中使用是否安全?例:

enum State {
  DoNot,
  Do,
};
State state;
std::mutex mutex;

// worker thread
std::thread th = std::thread([]()
{
    std::unique_lock<std::mutex> lc(mutex);
    cv.wait(lc, []() { return state; });
});

//signal handler
void handler(int sig)
{
    if (sig == SOME_SIG)
    {
        std::unique_lock<std::mutex> lc(mutex);
        state = Do;
        cv.notify_one();
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ posix signals c++11

8
推荐指数
1
解决办法
995
查看次数

标签 统计

c++ ×1

c++11 ×1

posix ×1

signals ×1