⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 posix线程编程指南(1).htm

📁 POSIX平台多线程编程指南 通用开放式操作系统的多线程应用指南。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
                  height=6 alt="" src="Posix线程编程指南(1).files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(1).files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=2><SPAN class=atitle>线程取消</SPAN></A></P>
            <P>
            <P><A name=N10095><SPAN class=smalltitle>2.1 线程取消的定义</SPAN></A></P>
            <P>一般情况下,线程在其主体函数退出的时候会自动终止,但同时也可以因为接收到另一个线程发来的终止(取消)请求而强制终止。</P>
            <P><A name=N1009E><SPAN class=smalltitle>2.2 线程取消的语义</SPAN></A></P>
            <P>线程取消的方法是向目标线程发Cancel信号,但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态决定。</P>
            <P>线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。</P>
            <P><A name=N100AA><SPAN class=smalltitle>2.3 取消点</SPAN></A></P>
            <P>根据POSIX标准,pthread_join()、pthread_testcancel()、pthread_cond_wait()、pthread_cond_timedwait()、sem_wait()、sigwait()等函数以及read()、write()等会引起阻塞的系统调用都是Cancelation-point,而其他pthread函数都不会引起Cancelation动作。但是pthread_cancel的手册页声称,由于LinuxThread库与C库结合得不好,因而目前C库函数都不是Cancelation-point;但CANCEL信号会使线程从阻塞的系统调用中退出,并置EINTR错误码,因此可以在需要作为Cancelation-point的系统调用前后调用pthread_testcancel(),从而达到POSIX标准所要求的目标,即如下代码段:</P>
            <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee 
            border=1>
              <TBODY>
              <TR>
                <TD><PRE><CODE class=section>pthread_testcancel();
    retcode = read(fd, buffer, length);
    pthread_testcancel();</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P><A name=N100B7><SPAN class=smalltitle>2.4 
程序设计方面的考虑</SPAN></A></P>
            <P>如果线程处于无限循环中,且循环体内没有执行至取消点的必然路径,则线程无法由外部其他线程的取消请求而终止。因此在这样的循环体的必经路径上应该加入pthread_testcancel()调用。</P>
            <P><A name=N100C0><SPAN class=smalltitle>2.5 
            与线程取消相关的pthread函数</SPAN></A></P>
            <P>int pthread_cancel(pthread_t thread) 
            <BR>发送终止信号给thread线程,如果成功则返回0,否则为非0值。发送成功并不意味着thread会终止。 </P>
            <P>int pthread_setcancelstate(int state, int *oldstate) 
            <BR>设置本线程对Cancel信号的反应,state有两种值:PTHREAD_CANCEL_ENABLE(缺省)和PTHREAD_CANCEL_DISABLE,分别表示收到信号后设为CANCLED状态和忽略CANCEL信号继续运行;old_state如果不为NULL则存入原来的Cancel状态以便恢复。 
            </P>
            <P>int pthread_setcanceltype(int type, int *oldtype) 
            <BR>设置本线程取消动作的执行时机,type由两种取值:PTHREAD_CANCEL_DEFFERED和PTHREAD_CANCEL_ASYCHRONOUS,仅当Cancel状态为Enable时有效,分别表示收到信号后继续运行至下一个取消点再退出和立即执行取消动作(退出);oldtype如果不为NULL则存入运来的取消动作类型值。 
            </P>
            <P>void pthread_testcancel(void) 
            <BR>检查本线程是否处于Canceld状态,如果是,则进行取消动作,否则直接返回。 </P><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="Posix线程编程指南(1).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(1).files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(1).files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="Posix线程编程指南(1).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(1).files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(1).files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=author><SPAN class=atitle>关于作者</SPAN></A></P>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD colSpan=3><IMG height=5 alt="" 
                  src="Posix线程编程指南(1).files/c.gif" width="100%"></TD></TR>
              <TR vAlign=top align=left>
                <TD>
                  <P></P></TD>
                <TD><IMG height=5 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width=4></TD>
                <TD width="100%">
                  <P><B>杨沙洲,</B>男,现攻读国防科大计算机学院计算机软件方向博士学位。您可以通过电子邮件 <A 
                  href="mailto:pubb@163.net">pubb@163.net</A>跟他联系。 
              </P></TD></TR></TBODY></TABLE><BR><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="Posix线程编程指南(1).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(1).files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(1).files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P class=no-print><SPAN class=atitle><A 
            name=rate>对本文的评价</A></SPAN></P><SPAN class=no-print>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR vAlign=top>
                <TD>
                  <FORM 
                  action=https://www-128.ibm.com/developerworks/secure/cnratings.jsp 
                  method=get><INPUT type=hidden value=Posix线程编程指南(1) 
                  name=ArticleTitle><INPUT type=hidden value=Linux 
                  name=Zone><INPUT type=hidden 
                  value=http://www.ibm.com/developerworks/cn/thankyou/ 
                  name=RedirectURL><INPUT type=hidden value=china 
name=localsite>
                  <SCRIPT language=javascript>document.write('<input type="hidden" name="url" value="'+location.href+'">');</SCRIPT>

                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD><IMG height=8 alt="" 
                        src="Posix线程编程指南(1).files/c.gif" width=100 
                    border=0></TD></TR>
                    <TR vAlign=top>
                      <TD><INPUT type=radio value=1 name=Rating>太差! (1)</TD></TR>
                    <TR vAlign=top>
                      <TD><INPUT type=radio value=2 name=Rating>需提高 (2)</TD></TR>
                    <TR vAlign=top>
                      <TD><INPUT type=radio value=3 name=Rating>一般;尚可 
(3)</TD></TR>
                    <TR vAlign=top>
                      <TD><INPUT type=radio value=4 name=Rating>好文章 (4)</TD></TR>
                    <TR vAlign=top>
                      <TD><INPUT type=radio value=5 
                    name=Rating>真棒!(5)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA id=Comments name=Comments rows=5 wrap=virtual cols=60>&nbsp;</TEXTAREA><BR><BR><INPUT type=submit value=反馈意见></FORM></TD></TR>
              <TR vAlign=top>
                <TD bgColor=#ffffff><IMG height=8 alt="" 
                  src="Posix线程编程指南(1).files/c.gif" width=100 
              border=0></TD></TR></TBODY></TABLE></SPAN><SPAN class=no-print>
            <TABLE cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=8 alt="" src="Posix线程编程指南(1).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(1).files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR></SPAN></TD>
          <TD width=10><IMG height=1 alt="" src="Posix线程编程指南(1).files/c.gif" 
            width=10></TD></TR></TBODY></TABLE>
      <P class=greytext>其他公司、产品或服务的名称可能是其他公司的商标或服务标志。</P></TD></TR></TBODY></TABLE><!--FOOTER_BEGIN--><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD class=bbg height=19>
      <TABLE cellSpacing=0 cellPadding=0 border=0>
        <TBODY>
        <TR>
          <TD><SPAN class=spacer>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><A 
            class=mainlink href="http://www.ibm.com/cn/ibm/index.shtml">关于 
            IBM</A></TD>
          <TD class=footer-divider width=27>&nbsp;&nbsp;&nbsp;&nbsp;</TD>
          <TD><A class=mainlink 
            href="http://www.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</A></TD>
          <TD class=footer-divider width=27>&nbsp;&nbsp;&nbsp;&nbsp;</TD>
          <TD><A class=mainlink href="http://www.ibm.com/contact/">联系 
          IBM</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2 src="Posix线程编程指南(1).files/stats.js" 
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt="" 
src="E:\MyLove\资料\Posix线程编程指南\Posix线程编程指南(1).files\c(1).gif" width=1 
border=0></NOSCRIPT><!--FOOTER_END--><!--XSLT stylesheet used to transform this file:  dw-document-html-5.3.xsl--> 
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -