- 论坛徽章:
- 0
|
原帖由 flw2 于 2007-11-24 23:05 发表
你要什么效果?没看出什么来,出了在信号处理函数中printf会导致死锁之外
我的系统printf是线程安全的,线程安全就意味着有锁,类似的还有malloc,信号处理函数中调用这些函数是错的
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <signal.h>
- void sig_fn(int signo){
- printf("pthread=%d exit\n",pthread_self());
- }
- void* fn(void *arg){
- int num=*(int*)arg;
- free(arg);
- signal(SIGALRM,sig_fn);
- pthread_kill(pthread_self(),SIGALRM);//这样就对啦,原来线程中使用信号跟进程是不一样的。不能在线程中用alarm来发信号,那是进程专用,alarm一下所有线程都收到了,要想单个线程收到信号要用pthread_kill。
- pthread_exit(0);
- }
- int main(){
- pthread_t ph1,ph2;
- int *num=(int*)malloc(sizeof(int));
- *num=1;
- pthread_create(&ph1,NULL,fn,num);
- int *num1=(int*)malloc(sizeof(int));
- *num1=2;
- pthread_create(&ph2,NULL,fn,num1);
- printf("main=%d\n",pthread_self());
- pthread_join(ph1,NULL);
- pthread_join(ph2,NULL);
- return 0;
- }
复制代码
2,难道只要是信号处理函数都不能用printf之类的函数?无论这个信号是在进程还是在线程中?
[ 本帖最后由 xxldc 于 2007-11-25 06:43 编辑 ] |
|