- 论坛徽章:
- 0
|
10可用积分
在win平台上,如果主线程执行结束了,子线程都会强制的退出。
下面这个solaris下面的pthread例程,子线程一直在执行。我的目的是按任意键程序就退出。运行都没有问题。- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- pthread_key_t key;
- void print(){printf("%s\n",(char*)pthread_getspecific(key));}
- extern "C" void* t(void* arg){
- pthread_setspecific(key,arg);
- while(1){
- print();
- sleep(1);
- }
- return NULL;
- }
- int main(void){
- pthread_t pa,pb;
- pthread_key_create(&key,NULL);
- pthread_create(&pa,NULL,t,(void*)"Thread a");
- pthread_create(&pb,NULL,t,(void*)"Thread b");
- // pthread_join(pa,NULL);
- // pthread_join(pb,NULL);
- getchar();
- return 0;
- }
复制代码 但是,我加上那两行的pthread_join(把注释去掉),那么不管我键盘敲什么,都不会退出。
pthread_join究竟干了什么事情? 我的主进程已经结束了,为什么子进程还不结束? |
最佳答案
查看完整内容
pthread_join()是把当前线程阻塞至目标线程结束才会继续运行,因为join的目的之一就是想得到目标线程退出状态。
|