PTHREAD_SIGMASK - Linux手册页
时间:2019-08-20 18:01:05 来源:igfitidea点击:
Linux程序员手册 第3部分
更新日期: 2020-06-09
名称
pthread_sigmask-检查并更改阻止信号的掩码
语法
#include <signal.h> int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
编译并链接-pthread。
glibc的功能测试宏要求(请参阅feature_test_macros(7)):
pthread_sigmask():
- _POSIX_C_SOURCE >>= 199506L || _XOPEN_SOURCE>= 500
说明
pthread_sigmask()函数类似于sigprocmask(2),不同之处在于它在多线程程序中的使用由POSIX.1明确指定。其他差异在此页中说明。
有关此函数的参数和操作的说明,请参见sigprocmask(2)。
返回值
成功时,pthread_sigmask()返回0;否则,返回0。如果出错,则返回错误号。
错误说明
请参阅sigprocmask(2)。
属性
有关本节中使用的术语的说明,请参见attribute(7)。
| Interface | Attribute | Value |
| pthread_sigmask() | Thread safety | MT-Safe |
遵循规范
POSIX.1-2001,POSIX.1-2008。
示例
下面的程序在主线程中阻止了一些信号,然后创建了一个专用线程来通过sigwait(3)获取这些信号。以下shell会话演示了其用法:
$ ./a.out & [1] 5423 $ kill -QUIT %1 Signal handling thread got signal 3 $ kill -USR1 %1 Signal handling thread got signal 10 $ kill -TERM %1 [1]+ Terminated ./a.out
Program source
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
/* Simple error handling functions */
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
sig_thread(void *arg)
{
sigset_t *set = arg;
int s, sig;
for (;;) {
s = sigwait(set, &sig);
if (s != 0)
handle_error_en(s, "sigwait");
printf("Signal handling thread got signal %d\n", sig);
}
}
int
main(int argc, char *argv[])
{
pthread_t thread;
sigset_t set;
int s;
/* Block SIGQUIT and SIGUSR1; other threads created by main()
will inherit a copy of the signal mask. */
sigemptyset(&set);
sigaddset(&set, SIGQUIT);
sigaddset(&set, SIGUSR1);
s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
handle_error_en(s, "pthread_sigmask");
s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
if (s != 0)
handle_error_en(s, "pthread_create");
/* Main thread carries on to create other threads and/or do
other work */
pause(); /* Dummy pause so we can test program */
}
另外参见
sigaction(2),sigpending(2),sigprocmask(2),pthread_create(3),pthread_kill(3),sigsetops(3),pthreads(7),signal(7)
出版信息
这个页面是Linux手册页项目5.08版的一部分。有关项目的说明、有关报告错误的信息以及此页面的最新版本,请访问https://www.kernel.org/doc/man-pages/。

