C语言 pthread_join()函数

Posted Dontla

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 pthread_join()函数相关的知识,希望对你有一定的参考价值。

man 1 pthread_join

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

NAME
       pthread_join - join with a terminated thread	//与终止的线程连接(调用者线程将阻塞,直到被join的线程返回)

SYNOPSIS
       #include <pthread.h>

       int pthread_join(pthread_t thread, void **retval);

       Compile and link with -pthread.

DESCRIPTION
       The  pthread_join()  function  waits  for  the  thread specified by thread to terminate.  If that thread has already terminated, then pthread_join() returns immediately.  The thread specified by thread must be joinable.
       //pthread_join() 函数等待线程指定的线程终止。 
       //如果该线程已经终止,则 pthread_join() 立即返回。 
       //thread 指定的线程必须是可连接的。

       If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target  thread  sup‐plied  to  pthread_exit(3))  into  the  location  pointed to by *retval.  If the target thread was canceled, then PTHREAD_CANCELED is placed in *retval.
       //如果 retval 不为 NULL,则 pthread_join() 将目标线程的退出状态(即目标线程提供给 pthread_exit(3) 的值)复制到 *retval 指向的位置。 
       //如果目标线程被取消,则 PTHREAD_CANCELED 被放置在 *retval 中。

       If multiple threads simultaneously try to join with the same thread, the results are undefined.  If the thread calling pthread_join() is canceled, then the target thread will remain joinable (i.e., it will not be detached).
       //如果多个线程同时尝试与同一个线程连接,则结果未定义。 
       //如果调用 pthread_join() 的线程被取消,那么目标线程将保持可被join()状态(即它不会detached())。

RETURN VALUE
       On success, pthread_join() returns 0; on error, it returns an error number.
       //成功时,pthread_join() 返回 0; 出错时,它返回一个错误号。

ERRORS
       EDEADLK
              A deadlock was detected (e.g., two threads tried to join with each other); or thread specifies the calling thread.

       EINVAL thread is not a joinable thread.

       EINVAL Another thread is already waiting to join with this thread.

       ESRCH  No thread with the ID thread could be found.

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

       ┌───────────────┬───────────────┬─────────┐
       │Interface      │ Attribute     │ Value   │
       ├───────────────┼───────────────┼─────────┤
       │pthread_join() │ Thread safety │ MT-Safe │
       └───────────────┴───────────────┴─────────┘

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008.

NOTES
       After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.
       //在成功调用 pthread_join() 后,调用者可以保证目标线程已经终止。

       Joining with a thread that has previously been joined results in undefined behavior.
       //join()之前已join()的线程会导致未定义的行为。

       Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".  Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie threads have accumulated, it will no longer be possible  to create new threads (or processes).
       //未能与可连接(joinable)的线程(即未分离的线程)连接会产生“僵尸线程”。 避免这样做,因为每个僵尸线程都会消耗一些系统资源,当累积了足够多的僵尸线程时,将无法再创建新线程(或进程)。

       There  is  no  pthreads  analog of waitpid(-1, &status, 0), that is, "join with any terminated thread".  If you believe you need this functionality, you probably need to rethink your application design.
       //没有类似于 waitpid(-1, &status, 0) 的 pthreads,即“join任何已终止的线程”。 
       //如果您认为您需要此功能,您可能需要重新考虑您的应用程序设计。

       All of the threads in a process are peers: any thread can join with any other thread in the process.
       //进程中的所有线程都是对等的:任何线程都可以与进程中的任何其他线程join。

EXAMPLE
       See pthread_create(3).

SEE ALSO
       pthread_cancel(3), pthread_create(3), pthread_detach(3), pthread_exit(3), pthread_tryjoin_np(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-07-23                                                  PTHREAD_JOIN(3)
 Manual page pthread_join(3) line 32/73 (END) (press h for help or q to quit)

以上是关于C语言 pthread_join()函数的主要内容,如果未能解决你的问题,请参考以下文章

c - 无法理解 pthread_join()

pthread_join的介绍

pthread_join - 多个线程等待

对线程等待函数pthread_join二级指针参数分析

pthread_exit&&pthread_join函数

如何在C中使用pthread_join来控制线程数?