#include
#include
#include
using namespace std;
pthread_key_t key;
void echomsg(void* p)
{
int t=*(int*)p;
printf( "destructor excuted in thread %d, param=%d\n ",pthread_self(),t);
}
void* child1(void* arg)
{
int*ptid= new int;
*ptid=pthread_self();
printf( "thread %d enter\n ",*ptid);
pthread_setspecific(key,(void*)ptid);
sleep(2);
printf( "thread %d returns %d\n ",*ptid,*((int*)pthread_getspecific(key)));
sleep(5);
pthread_exit(NULL);
return NULL;
}
void* child2(void* arg)
{
int*ptid= new int;
*ptid=pthread_self();
printf( "thread %d enter\n ",*ptid);
pthread_setspecific(key,(void*)ptid);
sleep(1);
printf( "thread %d returns %d\n ",*ptid,*((int*)pthread_getspecific(key)));
sleep(5);
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t tid1,tid2;
printf( "hello\n ");
pthread_key_create(&key,echomsg);
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
//pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sleep(3);
pthread_key_delete(key);
printf( "main thread %d exit\n ",pthread_self());
return 0;
}
这是在网上看到的一篇文章,个人认为并不是pthread_join接受线程时才调用每个线程的key的echomsg函数。而是由于pthread_join阻塞等待特定的线程结束,以至于被等待的线程能够全部处理完(当然包括每个线程key的特定清理函数echomsg),所以当pthread_join尤其用在main线程中时,能够确保特定的子线程能处理完。
在以上程序中,稍加修改:child2()函数中的sleep睡眠时间都改为sleep(1),并且把主函数的pthread_join(tid2,NULL);也注释掉,重新编译执行也会得到上面类似的结果。
同时也发现child1的线程也退出了,并没有sleep(5)足够的时间,我认为是child2线程结束时要发送信号,而sleep是可被信号中断的(这个陈述稍有欠当,姑且是那个意思),所以child1的线程在child2结束后也结束了。但是没来及执行child1的echomsg,main线程就结束了,随之整个进程也结束了
个人认为上面的程序有点隐形的bug,自己改进的代码如下:
#include
#include
#include
#include //atexit
using namespace std;
pthread_key_t key;
pthread_once_t thread_once = PTHREAD_ONCE_INIT;
void echomsg(void *);
void once_run(void)
{
printf("pthread_key_t init in the once_run\n ");
pthread_key_create(&key,echomsg);
}
void echomsg(void* p)
{
int t=*(int*)p;
printf( "destructor excuted in thread %d, param=%d\n ",pthread_self(),t);
delete (int *)p;
}
void* child1(void* arg)
{
int*ptid= new int;
pthread_once(&thread_once, once_run);//测试pthread_once是否还会在此执行不,因为在main线程已经执行了.
*ptid=pthread_self();
printf( "thread1 %d enter\n ",*ptid);
pthread_setspecific(key,(void*)ptid);
sleep(2);
printf( "thread1 %d returns %d\n ",*ptid,*((int*)pthread_getspecific(key)));
sleep(5);
pthread_exit(NULL);
return NULL;
}
void* child2(void* arg)
{
int*ptid= new int;
*ptid=pthread_self();
printf( "thread2 %d enter\n ",*ptid);
pthread_setspecific(key,(void*)ptid);
sleep(1);
printf( "thread2 %d returns %d\n ",*ptid,*((int*)pthread_getspecific(key)));
sleep(1);
pthread_exit(NULL);
return NULL;
}
void main_exit()
{
pthread_key_delete(key);
}
int main()
{
pthread_t tid1,tid2;
printf( "hello\n ");
atexit(main_exit);//为了防止sleep(4)放到pthread_key_delete(key)后面就会出现段错误了。但是个人也不很提倡用atexit这个函数。
pthread_once(&thread_once, once_run);
//pthread_key_create(&key,echomsg); //保证一次性运行把其放到了pthread_once了
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
//pthread_join(tid1,NULL);
//pthread_join(tid2,NULL);
sleep(3);
//pthread_key_delete(key);
printf( "main thread %d exit\n ",pthread_self());
return 0;
}
程序输出:
hello
pthread_key_t init in the once_run
thread1 -1208583280 enter
thread2 -1219073136 enter
thread2 -1219073136 returns -1219073136
thread1 -1208583280 returns -1208583280
destructor excuted in thread -1219073136, param=-1219073136
main thread -1208580400 exit
-到此结束-