linux C语言 pthread_t pthread_self()函数(获取调用线程的ID) pthread_equal() 线程id(thread ID)

Posted Dontla

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux C语言 pthread_t pthread_self()函数(获取调用线程的ID) pthread_equal() 线程id(thread ID)相关的知识,希望对你有一定的参考价值。

文章目录

man 3 pthread_self

PTHREAD_SELF(3)                                                      Linux Programmer's Manual                                                     PTHREAD_SELF(3)

NAME
       pthread_self - obtain ID of the calling thread	//获取调用线程的ID

SYNOPSIS
       #include <pthread.h>

       pthread_t pthread_self(void);

       Compile and link with -pthread.

DESCRIPTION
       The  pthread_self()  function  returns the ID of the calling thread.  This is the same value that is returned in *thread in the pthread_create(3) call that
       created this thread.
       //pthread_self() 函数返回调用线程的 ID。 
       //这与创建此线程的 pthread_create(3) 调用中的 *thread 返回的值相同。

RETURN VALUE
       This function always succeeds, returning the calling thread's ID.
       //此函数总是成功,返回调用线程的 ID。

ERRORS
       This function always succeeds.

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌───────────────┬───────────────┬─────────┐
       │Interface      │ Attribute     │ Value   │
       ├───────────────┼───────────────┼─────────┤
       │pthread_self() │ Thread safety │ MT-Safe │
       └───────────────┴───────────────┴─────────┘
CONFORMING TO
       POSIX.1-2001, POSIX.1-2008.

NOTES
       POSIX.1 allows an implementation wide freedom in choosing the type used to represent a thread ID; for example, representation using  either  an  arithmetic
       type  or  a  structure  is  permitted.   Therefore,  variables  of  type  pthread_t  can't  portably  be  compared  using the C equality operator (==); use
       pthread_equal(3) instead.
       //POSIX.1 允许实现在选择用于表示线程 ID 的类型方面有很大的自由度; 
       //例如,允许使用算术类型或结构表示。 
       //因此,pthread_t 类型的变量不能使用 C 相等运算符 (==) 进行可移植的比较; 请改用 pthread_equal(3)。

       Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is nonportable  and  can  lead  to  unspecified
       results.
       //线程标识符应该被认为是不透明的:
       //在 pthreads 调用中使用线程 ID 的任何尝试都是不可移植的,并且可能导致未指定的结果。
       //(就是说不能直接用线程数字替代pthread_t作为传入参数)

       Thread  IDs  are  guaranteed to be unique only within a process.  A thread ID may be reused after a terminated thread has been joined, or a detached thread
       has terminated.
       //线程 ID 保证仅在进程中是唯一的。 
       //在已终止的线程已加入或已分离的线程已终止后,可以重用线程 ID。

       The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to gettid(2).
       //pthread_self() 返回的线程 ID 与调用 gettid(2) 返回的内核线程 ID 不同。

SEE ALSO
       pthread_create(3), pthread_equal(3), pthreads(7)

COLOPHON
       This page is part of release 4.04 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the  latest  version
       of this page, can be found at http://www.kernel.org/doc/man-pages/.

Linux                                                                       2015-08-08                                                             PTHREAD_SELF(3)
 Manual page pthread_self(3) line 13/54 (END) (press h for help or q to quit)

后更。。。

以上是关于linux C语言 pthread_t pthread_self()函数(获取调用线程的ID) pthread_equal() 线程id(thread ID)的主要内容,如果未能解决你的问题,请参考以下文章

C语言多线程教程(pthread)(线程创建pthread_t,指定线程run方法pthread_create,加mutex锁,解锁,伪共享 false sharing假共享)

C语言线程函数参数问题

如何将pthread_t映射到pid(在Linux上)

linux多线程——基础知识+实现

linux多线程——基础知识+实现

Linux多线程操作pthread_t