Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;信号处理程序可以唤醒线程吗? P>由于C++信号处理程序只应访问易失性STD::SIGOMATICAICIt或STD::Ac原子(因为C++ 11),是否可以使用线程休眠和唤醒? std::atomic_bool exit_now(false); void signal_handler(int signal) { exit_now=true; } int main() { std::signal(SIGINT, signal_handler); A a; B b; a.RunAsync(); b.RunAsync(); while(!exit_now) std::this_thread::sleep_for(std::chrono::seconds(1)); a.Stop(); b.Stop(); return 0; }_C++_Multithreading_Signals - Fatal编程技术网

c++;信号处理程序可以唤醒线程吗? P>由于C++信号处理程序只应访问易失性STD::SIGOMATICAICIt或STD::Ac原子(因为C++ 11),是否可以使用线程休眠和唤醒? std::atomic_bool exit_now(false); void signal_handler(int signal) { exit_now=true; } int main() { std::signal(SIGINT, signal_handler); A a; B b; a.RunAsync(); b.RunAsync(); while(!exit_now) std::this_thread::sleep_for(std::chrono::seconds(1)); a.Stop(); b.Stop(); return 0; }

c++;信号处理程序可以唤醒线程吗? P>由于C++信号处理程序只应访问易失性STD::SIGOMATICAICIt或STD::Ac原子(因为C++ 11),是否可以使用线程休眠和唤醒? std::atomic_bool exit_now(false); void signal_handler(int signal) { exit_now=true; } int main() { std::signal(SIGINT, signal_handler); A a; B b; a.RunAsync(); b.RunAsync(); while(!exit_now) std::this_thread::sleep_for(std::chrono::seconds(1)); a.Stop(); b.Stop(); return 0; },c++,multithreading,signals,C++,Multithreading,Signals,在本例中,A和B::RunAsync()都在其他线程上执行它们的任务,直到我调用::Stop(),所以我唯一的选择是在主线程上忙着等待(有或没有预定义的睡眠周期) 理想情况下,我希望主线程在发出信号之前一直休眠,可能是使用条件变量,但这样做看起来是非法的。我建议(阻止信号)并使用sigwait(2)在主线程中同步等待信号,然后,您完全避免了必须从信号处理程序到线程进行通信的问题

在本例中,A和B::RunAsync()都在其他线程上执行它们的任务,直到我调用::Stop(),所以我唯一的选择是在主线程上忙着等待(有或没有预定义的睡眠周期)

理想情况下,我希望主线程在发出信号之前一直休眠,可能是使用条件变量,但这样做看起来是非法的。

我建议(阻止信号)并使用sigwait(2)在主线程中同步等待信号,然后,您完全避免了必须从信号处理程序到线程进行通信的问题