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
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> #define debug_Msg(fmt, arg...)\
do{\
printf("%s %d : ", __FILE__, __LINE__);\
printf(fmt, ##arg);\
}while() void * doPrint(void *arg)
{
debug_Msg("%s\n", (char*)arg);
char * p = "thread is over";
pthread_exit(p);
}
int main()
{
pthread_t pid;
char * pt = "hello pthread";
pthread_create(&pid, NULL, doPrint, pt);
void * p = NULL;
pthread_join(pid, &p);
debug_Msg("return of thread : [%s]\n", (char*)p); return ;
}
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 : hello pthread
xx_pthread_exit.c : return of thread : [thread is over]