<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0061)http://www.c-article.com/get/Linux-c924/Posix-a3387208_1.html -->
<HTML xmlns:o = "urn:schemas-microsoft-com:office:office" xmlns:v =
"urn:schemas-microsoft-com:vml"><HEAD><TITLE>[转载]Posix线程编程指南 _Linux</TITLE>
<META content=[转载]Posix线程编程指南 name=Description>
<META content="Linux, 操作系统, {ClassName}资料" name=Keywords>
<META http-equiv=content-type content=text/html;charset=GB2312><LINK
href="[转载]Posix线程编程指南 _Linux.files/NewA.css" type=text/css rel=stylesheet>
<SCRIPT language=javascript src="[转载]Posix线程编程指南 _Linux.files/NewJs.js"
type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY onmousemove=HideMenu()>
<DIV id=menuDiv
style="Z-INDEX: 1000; VISIBILITY: hidden; WIDTH: 0px; POSITION: absolute; HEIGHT: 0px; BACKGROUND-COLOR: #0164f9"></DIV>
<DIV class=DIVChannel
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"></DIV>
<DIV class=DIVBody style="MARGIN-TOP: 0px">
<DIV class=DIVChannel
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"><BR>
<DIV align=center>
<H1>[转载]Posix线程编程指南</H1></DIV>
<DIV
style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; BACKGROUND-COLOR: #ffffff">
<DIV
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; Z-INDEX: 100; FLOAT: right; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"></DIV>
<DIV>
<DIV>
<P class=Web1 style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt"><FONT
face=宋体><FONT size=3><SPAN class=7><B><SPAN
style="COLOR: #666666; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">线程创建与取消</SPAN></B></SPAN><SPAN
class=7><B><SPAN lang=EN-US
style="COLOR: #666666; FONT-FAMILY: Verdana"><o:p></o:p></SPAN></B></SPAN></FONT></FONT></P>
<P class=Web1 style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt"><B><SPAN
lang=EN-US style="COLOR: #666666; FONT-FAMILY: Verdana"><o:p><FONT
size=1> </FONT></o:p></SPAN></B></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN lang=EN-US><FONT face=Verdana>2001 </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">年</SPAN><SPAN
lang=EN-US><FONT face=Verdana> 10 </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">月</SPAN><SPAN
lang=EN-US><FONT face=Verdana> 01 </FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">日</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">这是一个关于</SPAN><SPAN
lang=EN-US><FONT face=Verdana>Posix</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">线程编程的专栏。作者在阐明概念的基础上,将向您详细讲述</SPAN><SPAN
lang=EN-US><FONT face=Verdana>Posix</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">线程库</SPAN><SPAN
lang=EN-US><FONT face=Verdana>API</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">。本文是第一篇将向您讲述线程的创建与取消。</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><A name=1><B><SPAN
style="FONT-SIZE: 13.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">线程创建</SPAN></B></A></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><A name=N10047><B><SPAN lang=EN-US
style="FONT-SIZE: 11.5pt; FONT-FAMILY: Arial; mso-bidi-font-size: 9.0pt">1</SPAN></B></A><SPAN
style="mso-bookmark: N10047"><B><SPAN
style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">.</SPAN></B></SPAN><SPAN
style="mso-bookmark: N10047"><B><SPAN lang=EN-US
style="FONT-SIZE: 11.5pt; FONT-FAMILY: Arial; mso-bidi-font-size: 9.0pt">1
</SPAN></B></SPAN><SPAN style="mso-bookmark: N10047"><B><SPAN
style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">线程与进程</SPAN></B></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列。在串行程序基础上引入线程和进程是为了提高程序的并发度,从而提高程序运行效率和响应时间。</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">线程和进程在使用上各有优缺点:线程执行开销小,但不利于资源的管理和保护;而进程正相反。同时,线程适合于在</SPAN><SPAN
lang=EN-US><FONT face=Verdana>SMP</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">机器上运行,而进程则可以跨机器迁移。</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><A name=N10053><B><SPAN lang=EN-US
style="FONT-SIZE: 11.5pt; FONT-FAMILY: Arial; mso-bidi-font-size: 9.0pt">1</SPAN></B></A><SPAN
style="mso-bookmark: N10053"><B><SPAN
style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">.</SPAN></B></SPAN><SPAN
style="mso-bookmark: N10053"><B><SPAN lang=EN-US
style="FONT-SIZE: 11.5pt; FONT-FAMILY: Arial; mso-bidi-font-size: 9.0pt">2
</SPAN></B></SPAN><SPAN style="mso-bookmark: N10053"><B><SPAN
style="FONT-SIZE: 11.5pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">创建线程</SPAN></B></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN lang=EN-US><FONT face=Verdana>POSIX</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">通过</SPAN><SPAN
lang=EN-US><FONT face=Verdana>pthread_create()</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">函数创建线程,</SPAN><SPAN
lang=EN-US><FONT face=Verdana>API</FONT></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">定义如下:</SPAN></P>
<TABLE class=MsoNormalTable
style="BACKGROUND: #eeeeee; WIDTH: 100%; mso-cellspacing: 0cm; mso-padding-alt: 3.75pt 3.75pt 3.75pt 3.75pt"
cellSpacing=0 cellPadding=0 width="100%" border=1>
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-lastrow: yes">
<TD
style="PADDING-RIGHT: 3.75pt; PADDING-LEFT: 3.75pt; BORDER-LEFT-COLOR: #d4d0c8; BORDER-BOTTOM-COLOR: #d4d0c8; PADDING-BOTTOM: 3.75pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 3.75pt; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #d4d0c8">
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 14.25pt; TE