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

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

📁 POSIX平台多线程编程指南 通用开放式操作系统的多线程应用指南。
💻 HTM
📖 第 1 页 / 共 3 页
字号:

pthread_once_t  once=PTHREAD_ONCE_INIT;

void    once_run(void)
{
        printf("once_run in thread %d\n",pthread_self());
}

void * child1(void *arg)
{
        int tid=pthread_self();
        printf("thread %d enter\n",tid);
        pthread_once(&once,once_run);
        printf("thread %d returns\n",tid);
}

void * child2(void *arg)
{
        int tid=pthread_self();
        printf("thread %d enter\n",tid);
        pthread_once(&once,once_run);
        printf("thread %d returns\n",tid);
}

int main(void)
{
        int tid1,tid2;

        printf("hello\n");
        pthread_create(&tid1,NULL,child1,NULL);
        pthread_create(&tid2,NULL,child2,NULL);
        sleep(10);
        printf("main thread exit\n");
        return 0;
}</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P>once_run()函数仅执行一次,且究竟在哪个线程中执行是不定的,尽管pthread_once(&amp;once,once_run)出现在两个线程中。</P>
            <P>LinuxThreads使用互斥锁和条件变量保证由pthread_once()指定的函数执行且仅执行一次,而once_control则表征是否执行过。如果once_control的初值不是PTHREAD_ONCE_INIT(LinuxThreads定义为0),pthread_once()的行为就会不正常。在LinuxThreads中,实际"一次性函数"的执行状态有三种:NEVER(0)、IN_PROGRESS(1)、DONE(2),如果once初值设为1,则由于所有pthread_once()都必须等待其中一个激发"已执行一次"信号,因此所有pthread_once()都会陷入永久的等待中;如果设为2,则表示该函数已执行过一次,从而所有pthread_once()都会立即返回0。</P><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="Posix线程编程指南(5).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(5).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线程编程指南(5).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(5).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/part5/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=4><SPAN 
            class=atitle>pthread_kill_other_threads_np()</SPAN></A></P>
            <P>
            <P>void pthread_kill_other_threads_np(void)</P>
            <P>这个函数是LinuxThreads针对本身无法实现的POSIX约定而做的扩展。POSIX要求当进程的某一个线程执行exec*系统调用在进程空间中加载另一个程序时,当前进程的所有线程都应终止。由于LinuxThreads的局限性,该机制无法在exec中实现,因此要求线程执行exec前手工终止其他所有线程。pthread_kill_other_threads_np()的作用就是这个。</P>
            <P>需要注意的是,pthread_kill_other_threads_np()并没有通过pthread_cancel()来终止线程,而是直接向管理线程发"进程退出"信号,使所有其他线程都结束运行,而不经过Cancel动作,当然也不会执行退出回调函数。尽管LinuxThreads的实验结果与文档说明相同,但代码实现中却是用的__pthread_sig_cancel信号来kill线程,应该效果与执行pthread_cancel()是一样的,其中原因目前还不清楚。</P><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="Posix线程编程指南(5).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(5).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线程编程指南(5).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(5).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/part5/#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线程编程指南(5).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(5).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线程编程指南(5).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(5).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/part5/#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线程编程指南(5).files/c.gif" width="100%"></TD></TR>
              <TR vAlign=top align=left>
                <TD>
                  <P></P></TD>
                <TD><IMG height=5 alt="" src="Posix线程编程指南(5).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线程编程指南(5).files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="Posix线程编程指南(5).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线程编程指南(5).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(5).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/part5/#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线程编程指南(5) 
                  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线程编程指南(5).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线程编程指南(5).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线程编程指南(5).files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="Posix线程编程指南(5).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/part5/#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线程编程指南(5).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线程编程指南(5).files/stats.js" 
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt="" 
src="E:\MyLove\资料\Posix线程编程指南\Posix线程编程指南(5).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 + -