您的位置:首页 > 运维架构 > Linux

linux下多线程之pthread_detach(pthread_self())

2016-07-11 08:44 351 查看
写个碰到的问题,记录下自己的技术之路点滴

pthread_detach(pthread_self())

linux线程执行和windows不同,pthread有两种状态joinable状态和unjoinable状态,

如果线程是joinable状态,当线程函数自己返回退出时或pthread_exit时都不会释放线程所占用堆栈和线程描述符(总计8K多)。只有当你调用了pthread_join之后这些资源才会被释放。

若是unjoinable状态的线程,这些资源在线程函数退出时或pthread_exit时自动会被释放。

unjoinable属性可以在pthread_create时指定,或在线程创建后在线程中pthread_detach自己, 如:pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放。或者将线程置为 joinable,然后适时调用pthread_join.

其实简单的说就是在线程函数头加上 pthread_detach(pthread_self())的话,线程状态改变,在函数尾部直接pthread_exit线程就会自动退出。省去了给线程擦屁股的麻烦

eg:

 pthread_t tid;

 int status = pthread_create(&tid, NULL, ThreadFunc, NULL);

 if(status != 0)

 {

  perror("pthread_create error");

 }

 pthread_detach(tid);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 线程