简体   繁体   English

是否有C ++标准库包装或替换pthread_sigmask?

[英]Is there a C++ standard library wrapper around or replacement for pthread_sigmask?

In the code base I am working on, all thread control is done using std::thread and related objects, rather than using pthread directly. 在我正在处理的代码库中,所有线程控制都是使用std::thread和相关对象完成的,而不是直接使用pthread

However, I have not been able to find a C++ equivalent of pthread_sigmask , so I am wondering if there is a standard C++ way to block signals, and thereby handle signals consistently in a multi-threaded application. 但是,我还没有找到与pthread_sigmask等效的C ++,所以我想知道是否有一种标准的C ++方法来阻止信号,从而在多线程应用程序中一致地处理信号。

In particular, I am wondering if there is a way to do the example here using c++11 without #include <pthread.h> 特别是,我想知道是否有办法没有#include <pthread.h>情况下使用c++11来做这个例子

Short answer: no. 简答:不。

The pthread_sigmask is a pthread function, and the C++ standard library of thread functions doesn't rely on a specific threading library. pthread_sigmask是一个pthread函数,而C ++标准的线程函数库不依赖于特定的线程库。 For example, a Windows implementation is likely to use the Windows APIs under the hood. 例如,Windows实现可能会使用Windows API。

If you want to use that function, you'll need to include <signal.h> and use it directly. 如果要使用该功能,则需要包含<signal.h>并直接使用它。 Even examples from Boost do this. 甚至Boost的例子都是这样做的。

Consider what you're trying to do, and ask yourself whether you really want to break portability by relying on a non-standard function or concept. 考虑一下你想要做什么,并问自己是否真的想通过依赖非标准功能或概念来打破可移植性。 Remember, threads and signals might not play nicely together; 请记住,线程和信号可能无法很好地协同工作; see Are std::signal and std::raise thread-safe? 看到std :: signal和std :: raise线程安全吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM