📄 posix线程编程指南(5).htm
字号:
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#1">获得本线程ID</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#2">判断两个线程是否为同一线程</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#3">仅执行一次的操作</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#4">pthread_kill_other_threads_np()</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#author">关于作者</A></TD></TR>
<TR class=left-nav-child-highlight>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/cl-bullet.gif"
width=2></TD>
<TD><A class=left-nav-child
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#rate">对本文的评价</A></TD></TR>
<TR class=left-nav-last>
<TD width=14><IMG class=display-img height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=14></TD>
<TD width=136><IMG class=display-img height=19 alt=""
src="Posix线程编程指南(5).files/left-nav-corner.gif"
width=136></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=related colSpan=2><B class=related>相关链接:</B></TD></TR>
<TR class=rlinks>
<TD><IMG height=8 alt="" src="Posix线程编程指南(5).files/rl-bullet.gif"
width=2></TD>
<TD><A class=rlinks
href="http://www.ibm.com/developerworks/cn/views/linux/articles.jsp">Linux
技术文档库</A></TD></TR><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- No content currently --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
<TR>
<TD width=14><IMG class=display-img height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=14></TD>
<TD width=136><IMG class=display-img height=19 alt=""
src="Posix线程编程指南(5).files/c.gif" width=136></TD></TR></TBODY></TABLE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- No content currently --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--></TD><!--LEFTNAV_END-->
<TD width="100%">
<TABLE id=content-table cellSpacing=0 cellPadding=0 width="100%"
border=0><TBODY>
<TR vAlign=top>
<TD width="100%">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><A name=main><IMG height=1 alt="skip to main content"
src="Posix线程编程指南(5).files/c.gif" width=592
border=0></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10 height=18><IMG height=18 alt=""
src="Posix线程编程指南(5).files/c.gif" width=10></TD>
<TD width="100%"><IMG height=6 alt=""
src="Posix线程编程指南(5).files/c.gif" width=1><BR><A class=bctl
href="http://www.ibm.com/developerworks/cn/">developerWorks
中国</A><SPAN class=bct> > </SPAN><A
class=bctl
href="http://www.ibm.com/developerworks/cn/linux/">Linux</A><SPAN
class=bct> ></SPAN><IMG height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=1><BR>
<H1>Posix线程编程指南(5)</H1>
<P id=subtitle><EM>杂项</EM></P><IMG class=display-img height=6
alt="" src="Posix线程编程指南(5).files/c.gif" width=1></TD>
<TD class=no-print width=192><IMG height=18 alt=developerWorks
src="Posix线程编程指南(5).files/dw.gif"
width=192></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width=10><IMG height=1 alt="" src="Posix线程编程指南(5).files/c.gif"
width=10></TD>
<TD width="100%">
<TABLE class=no-print cellSpacing=0 cellPadding=0 width=160
align=right border=0>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=10></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-1-small>文档选项</TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0
border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143
border=0><IMG height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=8>
<FORM name=email
action=https://www-128.ibm.com/developerworks/secure/email-it.jsp><INPUT
type=hidden
value=这是一个关于Posix线程编程的专栏。作者在阐明概念的基础上,将向您详细讲述Posix线程库API。本文是第五篇将向您讲述pthread_self()、pthread_equal()和pthread_once()等杂项函数。
name=body><INPUT type=hidden value=Posix线程编程指南(5)
name=subject><INPUT type=hidden value=cn name=lang>
<SCRIPT language=JavaScript type=text/javascript><!--document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>');//--></SCRIPT>
<NOSCRIPT>
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=8></TD>
<TD width=16><IMG height=16 alt=""
src="Posix线程编程指南(5).files/c.gif" width=16></TD>
<TD class=small width=122>
<P><SPAN class=ast>未显示需要 JavaScript
的文档选项</SPAN></P></TD></TR></NOSCRIPT></FORM></TBODY></TABLE></TD></TR></TBODY></TABLE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- 03/20/06 updated by gretchen --><BR>
<TABLE cellSpacing=0 cellPadding=0 width=150 border=0>
<TBODY>
<TR>
<TD class=v14-header-2-small>最新推荐</TD></TR></TBODY></TABLE>
<TABLE class=v14-gray-table-border cellSpacing=0 cellPadding=0
border=0>
<TBODY>
<TR>
<TD class=no-padding width=150>
<TABLE cellSpacing=0 cellPadding=0 width=143 border=0>
<TBODY>
<TR vAlign=top>
<TD width=8><IMG height=1 alt=""
src="Posix线程编程指南(5).files/c.gif" width=8></TD>
<TD><IMG height=16 alt=""
src="Posix线程编程指南(5).files/fw_bold.gif" width=16
vspace=3 border=0></TD>
<TD width=125>
<P><A class=smallplainlink
href="http://www-128.ibm.com/developerworks/cn/kickstart/">Java
应用开发源动力 - 下载免费软件,快速启动开发</A>
</P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><BR></TD></TR></TBODY></TABLE>
<P>级别: 初级</P>
<P><A
href="http://www-128.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part5/#author">杨沙洲</A>
<BR></P>
<P>2001 年 11 月 01 日</P>
<BLOCKQUOTE>这是一个关于Posix线程编程的专栏。作者在阐明概念的基础上,将向您详细讲述Posix线程库API。本文是第五篇将向您讲述pthread_self()、pthread_equal()和pthread_once()等杂项函数。</BLOCKQUOTE><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
<P>在Posix线程规范中还有几个辅助函数难以归类,暂且称其为杂项函数,主要包括pthread_self()、pthread_equal()和pthread_once()三个,另外还有一个LinuxThreads非可移植性扩展函数pthread_kill_other_threads_np()。本文就介绍这几个函数的定义和使用。</P>
<P><A name=1><SPAN class=atitle>获得本线程ID</SPAN></A></P>
<P>
<P>pthread_t pthread_self(void)</P>
<P>本函数返回本线程的标识符。</P>
<P>在LinuxThreads中,每个线程都用一个pthread_descr结构来描述,其中包含了线程状态、线程ID等所有需要的数据结构,此函数的实现就是在线程栈帧中找到本线程的pthread_descr结构,然后返回其中的p_tid项。</P>
<P>pthread_t类型在LinuxThreads中定义为无符号长整型。</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=2><SPAN class=atitle>判断两个线程是否为同一线程</SPAN></A></P>
<P>
<P>int pthread_equal(pthread_t thread1, pthread_t thread2)</P>
<P>判断两个线程描述符是否指向同一线程。在LinuxThreads中,线程ID相同的线程必然是同一个线程,因此,这个函数的实现仅仅判断thread1和thread2是否相等。</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=3><SPAN class=atitle>仅执行一次的操作</SPAN></A></P>
<P>
<P>int pthread_once(pthread_once_t *once_control, void
(*init_routine) (void))</P>
<P>本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次。</P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee
border=1>
<TBODY>
<TR>
<TD><PRE><CODE class=section>#include <stdio.h>
#include <pthread.h>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -