pthread_join 两次同一个线程
2016-12-10 18:13
429 查看
现象:我先创建两个线程 A和B,将其线程ID保存下来。 然后依次pthread_join 这两个线程。然后创建一个新的线程C。
然后我再次pthread_join 线程A和B。 结果发现他们两个之中一个会返回成功。
原因:
因为线程C的线程ID和线程A和B之中的一个相同。导致我对同一个线程pthread_join 两次,分别pthread_join 了不同的线程。
措施:
在调用pthread_join 的时候进行判断是否已经pthread_join 了,如果已经joni了,就不再join。如果这样做则需要考虑多线程同时访问。
man pthread_join :有如下提示
After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.
Joining with a thread that has previously been joined results in undefined behavior.
All of the threads in a process are peers: any thread can join with any other thread in the process.
然后我再次pthread_join 线程A和B。 结果发现他们两个之中一个会返回成功。
原因:
因为线程C的线程ID和线程A和B之中的一个相同。导致我对同一个线程pthread_join 两次,分别pthread_join 了不同的线程。
措施:
在调用pthread_join 的时候进行判断是否已经pthread_join 了,如果已经joni了,就不再join。如果这样做则需要考虑多线程同时访问。
man pthread_join :有如下提示
After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.
Joining with a thread that has previously been joined results in undefined behavior.
All of the threads in a process are peers: any thread can join with any other thread in the process.
相关文章推荐
- pthread_join函数及linux线程
- 使用pthread_cond_timedwait把一个线程作为一个定时器使用.
- Linux线程-pthread_join
- c线程中sleep()和pthread_join()函数之我见
- 6、一个 pthread_cancel 引起的线程死锁(转)
- pthread_join函数及linux线程
- pthread_join函数及其它线程终止函数
- 6、一个 pthread_cancel 引起的线程死锁【整理转载】
- 等待线程退出pthread_join
- pthread 线程冲突一个简单例子
- 原创 使用pthread_cond_timedwait把一个线程作为一个定时器使用.
- pthread_join多线程中关于一个二级指针
- pthread_join函数及linux线程
- pthread_join函数及linux线程
- 线程pthread_join和分离式线程
- pthread_join函数及linux线程转
- pthread_cancel,取消一个线程
- 随笔日记: 一个daemon程序,它的pthread_join()经常意外返回, 很是奇怪!
- .net C#线程连接Join的技巧,如何真正让一个线程结束,另外一个线程启动
- c线程中sleep()和pthread_join()函数之我见