【发布时间】:2015-01-17 07:31:32
【问题描述】:
这是我的代码:
void handler(int sig)
{
printf("%lu recv signal\n", pthread_self());
}
void* thread_fun(void *threadid)
{
printf("thread %lu created\n", pthread_self());
while(1){
sleep(1);
}
return NULL;
}
int main(void)
{
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);
printf("thread %lu created\n", pthread_self());
pthread_t t1,t2;
pthread_create(&t1, NULL, thread_fun, NULL);
pthread_create(&t2, NULL, thread_fun, NULL);
while(1)
sleep(1);
return 0;
}
APUE 表示在多线程进程中,像 SIGINT 这样的信号将被传递到随机线程中,但是,当我在 Ubuntu 14.04 上运行此代码时,似乎总是将信号传递到主线程线。有谁知道问题出在哪里?
【问题讨论】:
-
正式地,您不应该在信号处理程序中调用
printf()。见How to avoid callingprintf()in a signal handler?
标签: c multithreading unix signals posix