c ++ 11在信号处理程序中使用条件变量 [英] c++11 use condition variable in signal handler
问题描述
使用std::condition_variable::notify_one<是否安全/code>
在信号处理程序中?示例:
Is it safe to use std::condition_variable::notify_one
in signal handler? Example:
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 说:
A C++14 draft standard N4296 says:
[support.runtime]/10 C 和 C++ 语言的公共子集由所有声明、定义和表达式组成它可能出现在格式良好的 C++ 程序中,也可能出现在符合标准的 C 程序中.POF(普通的旧函数")是一个只使用来自这个公共子集的特征的函数,并且不直接或间接使用任何不是 POF 的函数,除了它可能使用普通的无锁原子操作......行为任何在 C++ 程序中用作信号处理程序的 POF 以外的函数是实现定义的.
[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.
强调我的.
这篇关于c ++ 11在信号处理程序中使用条件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!