赞
踩
一:线程信号相关函数
1.pthread_kill()函数
该函数其实不是kill线程,而是向线程发送一个signal(线程所有函数中最名不符其实的函数吧。。。)
成功返回0,失败返回失败码。
第一个参数:要发送信号的线程tid。
第二个参数:0是保留信号,用来判断线程是否还存在
sig信号 (在/usr/include/bits/signum.h中)
二、代码
- #include<stdio.h>
- #include<unistd.h>
- #include<pthread.h>
- #include<signal.h>//SIGQUIT /usr/include/bits/signum.h
- #include<errno.h>// ESRCH /usr/include/asm-/error-bash.h
-
- void* thread_fun(void* arg)
- {
- // sleep(1);
- printf("i am new thread.\n");
- return (void*)0;
- }
-
- int main(int argc ,char *argv[])
- {
- pthread_t tid;
- int err;
- int res_kill;
-
- err = pthread_create(&tid, NULL, thread_fun, NULL);
- if(err != 0)
- {
- printf("new thread create is failed.\n");
- return 0;
- }
- sleep(1);
- //pthread_kill()
- res_kill = pthread_kill(tid, 0);//0 signal is retain sign,then no signal is send
- // res_kill = pthread_kill(tid, SIGQUIT);//SIGQUIT signal is quit signal ,it will quit main process
- if(res_kill == ESRCH)
- {
- printf("new thread tid is not found.\n");
- printf("ret_kill = %d\n",res_kill);
- }
- // void * retval;
- // int thread_join = pthread_join(tid, &retval);
- printf("i am main thread .\n");
- return 0;
- }
运行代码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。