原文:点击打开链接http://blog.youkuaiyun.com/lovecodeless/article/details/23956975
Pthread
是 POSIX threads 的简称,是POSIX的
线程标准
。
终止线程似乎是多线程编程的最后一步,但绝不是本系列教材的结束。线程创建到线程终止,希望先给读者一个关于多线程编程的整体认识。
1.终止Pthread线程:pthread_exit()
参数:
ret:地址指针,本质上是返回值写入的地址。
终止线程是线程的主动行为,一个线程调用pthread_exit,终止线程自身。线程终止会释放线程特定数据,线程特定数据为线程专有。由于线程共享全局数据,故线程退出不会释放进程的全局数据。
该函数返回值ret如何使用?返回值是调用的概念,那么只有在一个线程被另一个线程“调用”时返回值ret才起作用。这里“调用”不同于一般意义上的函数调用,一个线程等待一个线程可以理解为“调用”。如一个线程调用
pthread_join等待另一个线程终止。下面介绍等待线程终止函数pthread_join。
2.等待线程终止:pthread_join()
该返回值ret通过另一个函数pthread_join传递。等待线程终止pthread_join原型为:
等待线程终止pthread_join会阻塞调用线程,直到其指定的线程终止。pthread_join通过第一个参数:线程ID来指定线程。调用者调用pthread_jion等待一个特定线程终止,在这种情况下,调用者可能需要这个特定线程的返回值,pthread_join通过将value_ptr的地址赋值给特定线程的pthread_exit的ret获取返回值。
3.pthread_exi与pthread_join牛刀小试:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a> <a target=_blank id="L2" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a> <a target=_blank id="L3" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a> <a target=_blank id="L4" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a> <a target=_blank id="L5" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a> <a target=_blank id="L6" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a> <a target=_blank id="L7" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a> <a target=_blank id="L8" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a> <a target=_blank id="L9" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;"> 9</a> <a target=_blank id="L10" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a> <a target=_blank id="L11" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a> <a target=_blank id="L12" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a> <a target=_blank id="L13" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a> <a target=_blank id="L14" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a> <a target=_blank id="L15" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a> <a target=_blank id="L16" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a> <a target=_blank id="L17" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a> <a target=_blank id="L18" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a> <a target=_blank id="L19" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a> <a target=_blank id="L20" href="http://blog.youkuaiyun.com/lovecodeless/article/details/23956975#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a> |
来自CODE的代码片
exit_join.c
上面的例子主线程main调用pthread_join等待子线程My_thread线程终止,通过传递My_thread_ret地址获取子线程My_thread的返回值,最后在屏幕上输出获得的返回值。