Linux多线程实例练习 - pthread_exit() 与 pthread_join()

Linux多线程实例练习 - pthread_exit 与 pthread_join

pthread_exit():终止当前线程

void pthread_exit(void* retval);

pthread_join():阻塞当前的线程,直到另外一个线程运行结束

int pthread_join(pthread_t thread, void **retval);

 

1、代码  xx_pthread_exit.c

复制代码
 1 #include <pthread.h>
 2 #include <stdio.h>
 3 #include <unistd.h>
 4 
 5 #define debug_Msg(fmt, arg...)\
 6     do{\
 7         printf("%s %d : ", __FILE__, __LINE__);\                                                     
 8         printf(fmt, ##arg);\
 9     }while(0)
10 
11 void * doPrint(void *arg)
12 {
13     debug_Msg("%s\n", (char*)arg);
14     char * p = "thread is over";
15     pthread_exit(p);
16 }
17 int main()
18 {
19     pthread_t pid;
20     char * pt = "hello pthread";
21     pthread_create(&pid, NULL, doPrint, pt);
22     void * p = NULL;
23     pthread_join(pid, &p);
24     debug_Msg("return of thread : [%s]\n", (char*)p);
25     
26     return 0;
27 }
复制代码

2、CentOS 下编译通过

g++ -g -c -o xx_pthread_exit.o xx_pthread_exit.c 
g++ -g -o xx_pthread_exit xx_pthread_exit.o -lpthread

3、运行结果

$ ./xx_pthread_exit 
xx_pthread_exit.c 13 : hello pthread
xx_pthread_exit.c 24 : return of thread : [thread is over]

 


本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4258917.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值