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

前端之家收集整理的这篇文章主要介绍了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();
    }
}

解决方法

C 14标准草案 N4296说:

[support.runtime]/10 The common subset of the C and C++ languages consists of all declarations,definitions,and expressions
that may appear in a well formed C++ program and also in a conforming C program. A POF (“plain old
function”) is a function that uses only features from this common subset,and that does not directly or indirectly use any function that is not a POF,except that it may use plain lock-free atomic operations… The behavior of any
function other than a POF used as a signal handler in a C++ program is implementation-defined.

强调我的.

原文链接:https://www.f2er.com/c/118889.html

猜你在找的C&C++相关文章