线程通过调用
voidpthread_exit(void*retval)
函数终止执行,就如同进程在结束时调用
exit
函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针,该返回值可以通过
pthread_join
函数的第二个参数得到
。
当然执行
pthread_exit
函数时,通过
pthread_cleanup_push压栈的函数会首先被执行,然后如果还有
pthread_key_create(pthread_key_t *keyptr, void (* destructor)(void *value))中的
pthread_key_t
*keyptr
和非null的数据和它相关联,
void (* destructor)(void *value)
紧接着会被执行。关于
pthread_key_create
的更多内容请参考《
Posix线程私有数据
》
<wbr style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; background-color:rgb(245,245,245)"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; background-color:rgb(245,245,245)">示例1</span> <div style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; background-color:rgb(245,245,245)"> <pre class="prettyprint" style="padding:2px; border:1px solid rgb(136,136,136)"><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"></p><div> <span class="pun" style="color:rgb(102,102,0)"> </span><span class="com" style="color:rgb(136,0,0)">#include</span><span class="pln"> </span><span class="str" style="color:rgb(0,136,0)"><stdio.h></span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">#include</span><span class="pln"> </span><span class="str" style="color:rgb(0,136,0)"><stdlib.h></span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">#include</span><span class="pln"> </span><span class="str" style="color:rgb(0,136,0)"><pthread.h></span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">print_message_function</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">ptr </span><span class="pun" style="color:rgb(102,102,0)">)</span> <span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">{</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">message</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> message </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*)</span><span class="pln"> ptr</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"%s \t"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> message</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"PID: %ld \n"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> pthread_self</span><span class="pun" style="color:rgb(102,102,0)">());</span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">/* "thread all done"的指针可以通过pthread_join的第二个参数取得*/</span> <span class="pln"> pthread_exit </span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"thread all done"</span><span class="pun" style="color:rgb(102,102,0)">);</span><span class="pln"> </span> <span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">}</span> <span class="pln"> main</span><span class="pun" style="color:rgb(102,102,0)">()</span> <span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">{</span> <span class="pln"> </span><span class="typ" style="color:rgb(102,0,102)">pthread_t</span><span class="pln"> thread1</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> thread2</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">message1 </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> </span><span class="str" style="color:rgb(0,136,0)">"Thread 1"</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">message2 </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> </span><span class="str" style="color:rgb(0,136,0)">"Thread 2"</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pln"> iret1</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> iret2</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">pth_join_ret1</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*</span><span class="pln">pth_join_ret2</span><span class="pun" style="color:rgb(102,102,0)">;</span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">/* Create independant threads each of which will execute function */</span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">/*pthread_create return 0 if create a thread is ok!*/</span> <span class="pln"> iret1 </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> pthread_create</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">&</span><span class="pln">thread1</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> NULL</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> print_message_function</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pun" style="color:rgb(102,102,0)">*)</span><span class="str" style="color:rgb(0,136,0)">"thread one_here"</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> iret2 </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> pthread_create</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">&</span><span class="pln">thread2</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> NULL</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> print_message_function</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pun" style="color:rgb(102,102,0)">*)</span><span class="pln"> message2</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> pthread_join</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln"> thread1</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">&</span><span class="pln">pth_join_ret1</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> pthread_join</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln"> thread2</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">&</span><span class="pln">pth_join_ret2</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">/*</span> <span class="com" style="color:rgb(136,0,0)"> if(pth_join_ret1==NULL || pth_join_ret2==NULL)</span> <span class="com" style="color:rgb(136,0,0)"> {</span> <span class="com" style="color:rgb(136,0,0)"> printf("in %d lines \n",__LINE__);</span> <span class="com" style="color:rgb(136,0,0)"> }</span> <span class="com" style="color:rgb(136,0,0)"> */</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"Thread 1 returns: %d\n"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln">iret1</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"Thread 2 returns: %d\n"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln">iret2</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> </span><span class="com" style="color:rgb(136,0,0)">/*打印 线程退出时的返回值*/</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"pthread_join 1 returns: %s\n"</span><span class="pun" style="color:rgb(102,102,0)">,(</span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*)</span><span class="pln">pth_join_ret1</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> printf</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"pthread_join 2 returns: %s\n"</span><span class="pun" style="color:rgb(102,102,0)">,(</span><span class="kwd" style="color:rgb(0,0,136)">char</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">*)</span><span class="pln">pth_join_ret2</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">exit</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="lit" style="color:rgb(0,102,102)">0</span><span class="pun" style="color:rgb(102,102,0)">);</span> <span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">}</span> </div><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"></p></pre> </div> <div style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; background-color:rgb(245,245,245)"> 结束!</div> </wbr>