pthread_create简介

原文:<wbr><a rel="nofollow" href="http://baike.baidu.com/view/1797052.htm" style="color:rgb(245,149,19); text-decoration:none">http://baike.baidu.com/view/1797052.htm</a> <div> <div><strong>函数简介</strong></div> <div> <span style="color:#000080">  </span><span style="color:#993300">pthread_create</span><span style="color:#000080">是UNIX环境用于创建线程函数</span> </div> <div><strong>头文件</strong></div> <div><span style="color:#000080">  #include&lt;pthread.h&gt;</span></div> <div><strong>函数声明</strong></div> <div> <span style="color:#000080">  int</span><span style="color:#993300">pthread_create</span><span style="color:#000080">(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);</span> </div> <div><strong>返回值</strong></div> <div><span style="color:#000080">  若成功则返回0,否则返回出错编号</span></div> <div><span style="color:#000080">  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。</span></div> <div><span style="color:#000080">  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。</span></div> <div> <span style="color:#000080">  </span><span style="line-height:22px"><span style="color:#ff00ff; line-height:25px">restrict</span><span style="color:#003366; line-height:25px">是C99引入的新关键字,它用于</span></span><span style="color:#003366">声明只会通过该指针对其指向的内存空间进行读写操作,以便编译器能进行更好的优化。</span> </div> <div>关于<span style="color:#993300">restrict</span>的更多内容请阅读《<a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201251391025211/" style="color:rgb(245,149,19); text-decoration:none; white-space:nowrap">关键字restrict简介</a>》<span style="color:#000080">。</span> </div> <div><strong>参数</strong></div> <div> <span style="color:#000080">  第一个参数指向用于存放线程标识符的pthread_t变量</span><span style="color:rgb(0,0,128)">。</span> </div> <div><span style="color:#000080">  第二个参数用来设置线程属性。</span></div> <div><span style="color:#000080">  第三个参数是线程运行函数的起始地址。</span></div> <div><span style="color:#000080">  最后一个参数是运行函数的参数。</span></div> <div> <span style="color:#000080">  另外,在编译时注意加上</span><span style="color:#993300">-lpthread</span><span style="color:#000080">参数,以调用静态链接库。因为pthread并非Linux系统的默认库</span> </div> <div><strong>示例1</strong></div> </div> <div> <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="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)">&lt;pthread.h&gt;</span> </div><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)">&lt;stdlib.h&gt;</span> </div><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)">&lt;stdio.h&gt;</span> </div><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)">&lt;unistd.h&gt;</span> </div><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)">&lt;string.h&gt;</span> </div><div> <span class="pun" style="color:rgb(102,102,0)">  </span><span class="typ" style="color:rgb(102,0,102)">pthread_t</span><span class="pln"> ntid</span><span class="pun" style="color:rgb(102,102,0)">;</span> </div><div> <span class="pun" style="color:rgb(102,102,0)">  </span><span class="kwd" style="color:rgb(0,0,136)">void</span><span class="pln"> printids</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="kwd" style="color:rgb(0,0,136)">const</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">s</span><span class="pun" style="color:rgb(102,102,0)">)</span> </div><div><span class="pun" style="color:rgb(102,102,0)">  {</span></div><div> <span class="pln"> </span><span class="pln">  </span><span class="typ" style="color:rgb(102,0,102)">pid_t</span><span class="pln"> pid</span><span class="pun" style="color:rgb(102,102,0)">;</span> </div><div> <span class="pln"> </span><span class="pln">  </span><span class="typ" style="color:rgb(102,0,102)">pthread_t</span><span class="pln"> tid</span><span class="pun" style="color:rgb(102,102,0)">;</span> </div><div> <span class="pln"> </span><span class="pln">  pid </span><span class="pun" style="color:rgb(102,102,0)">=</span><span class="pln"> getpid</span><span class="pun" style="color:rgb(102,102,0)">();</span> </div><div> <span class="pln"> </span><span class="pln">  tid </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> </div><div> <span class="pln"> </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 pid %u tid %u (0x%x)\n"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> s</span><span class="pun" style="color:rgb(102,102,0)">,(</span><span class="kwd" style="color:rgb(0,0,136)">unsigned</span><span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pun" style="color:rgb(102,102,0)">)</span><span class="pln">pid</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)">unsigned</span><span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pun" style="color:rgb(102,102,0)">)</span><span class="pln">tid</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)">unsigned</span><span class="pln"> </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pun" style="color:rgb(102,102,0)">)</span><span class="pln">tid</span><span class="pun" style="color:rgb(102,102,0)">);</span> </div><div> <span class="pun" style="color:rgb(102,102,0)">  }</span><span class="pln"></span> </div><div> <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">thr_fn</span><span class="pun" style="color:rgb(102,102,0)">(</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">arg</span><span class="pun" style="color:rgb(102,102,0)">)</span> </div><div><span class="pun" style="color:rgb(102,102,0)">  {</span></div><div> <span class="pln"> </span><span class="pln">  printids</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"new thread: "</span><span class="pun" style="color:rgb(102,102,0)">);</span> </div><div> <span class="pln"> </span><span class="pln">  </span><span class="kwd" style="color:rgb(0,0,136)">return</span><span class="pun" style="color:rgb(102,102,0)">((</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="lit" style="color:rgb(0,102,102)">0</span><span class="pun" style="color:rgb(102,102,0)">);</span> </div><div><span class="pun" style="color:rgb(102,102,0)">  }</span></div><div> <span class="pun" style="color:rgb(102,102,0)">  </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pln"> main</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> </div><div><span class="pun" style="color:rgb(102,102,0)">  {</span></div><div> <span class="pln"> </span><span class="pln">  </span><span class="kwd" style="color:rgb(0,0,136)">int</span><span class="pln"> err</span><span class="pun" style="color:rgb(102,102,0)">;</span> </div><div> <span class="pln"> </span><span class="pln">  err </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)">(&amp;</span><span class="pln">ntid</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"> thr_fn</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> </div><div> <span class="pln"> </span><span class="pln">  </span><span class="kwd" style="color:rgb(0,0,136)">if</span><span class="pln"> </span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln">err </span><span class="pun" style="color:rgb(102,102,0)">!=</span><span class="pln"> </span><span class="lit" style="color:rgb(0,102,102)">0</span><span class="pun" style="color:rgb(102,102,0)">)</span> </div><div> <span class="pln"> </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)">"can't create thread: %s\n"</span><span class="pun" style="color:rgb(102,102,0)">,</span><span class="pln"> strerror</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="pln">err</span><span class="pun" style="color:rgb(102,102,0)">));</span> </div><div> <span class="pln"> </span><span class="pln">  printids</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="str" style="color:rgb(0,136,0)">"main thread:"</span><span class="pun" style="color:rgb(102,102,0)">);</span> </div><div> <span class="pln"> </span><span class="pln">  sleep</span><span class="pun" style="color:rgb(102,102,0)">(</span><span class="lit" style="color:rgb(0,102,102)">1</span><span class="pun" style="color:rgb(102,102,0)">);</span> </div><div> <span class="pln"> </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> </div><div><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> <div><span style="color:#000080"><span style="white-space:pre"></span>执行以下命令进行编译</span></div> <div><span style="color:#3366ff"><span style="white-space:pre"></span>$ gcc main.c -lpthread</span></div> <div><span style="color:#000080"><span style="white-space:pre"></span>运行</span></div> <div><span style="color:#3366ff">  $ ./a.out</span></div> <div><span style="color:#000080"><span style="white-space:pre"></span>运行结果如下:</span></div> <div><span style="color:#3366ff">  main thread: pid 7398 tid 3084450496 (0xb7d8fac0)</span></div> <div><span style="color:#3366ff">  new thread: pid 7398 tid 3084446608 (0xb7d8eb90)</span></div> </div> </wbr>

你可能感兴趣的:(pthread)