---
name: "pthread_join"
section: "3thr"
platform: "linux"
package: "glibc-doc"
version: "2.1.3-20"
release: "Debian 2.2 (Potato)"
platforms: ["linux"]
url: "https://new.carta.tech/pthread_join/3thr"
---

# pthread\_join(3thr)

wait for termination of another thread

## NAME

pthread\_join - wait for termination of another thread

## SYNOPSIS

**#include \<pthread.h\>**

**int pthread\_join(pthread\_t** *th***, void \*\****thread\_return***);**

## DESCRIPTION

**pthread\_join** suspends the execution of the calling thread until the thread identified by  *th* terminates, either by calling  [**pthread\_exit**(3)](/pthread_exit/3.md) or by being cancelled.

If  *thread\_return* is not  **NULL**, the return value of  *th* is stored in the location pointed to by  *thread\_return*. The return value of *th* is either the argument it gave to  [**pthread\_exit**(3)](/pthread_exit/3.md), or **PTHREAD\_CANCELED** if  *th* was cancelled.

The joined thread  **th** must be in the joinable state: it must not have been detached using  [**pthread\_detach**(3)](/pthread_detach/3.md) or the **PTHREAD\_CREATE\_DETACHED** attribute to  [**pthread\_create**(3)](/pthread_create/3.md).

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs  **pthread\_join** on it. Therefore,  **pthread\_join** must be called once for each joinable thread created to avoid memory leaks.

At most one thread can wait for the termination of a given thread. Calling  **pthread\_join** on a thread  *th* on which another thread is already waiting for termination returns an error.

## CANCELLATION

**pthread\_join** is a cancellation point. If a thread is canceled while suspended in  **pthread\_join**, the thread execution resumes immediately and the cancellation is executed without waiting for the  *th* thread to terminate. If cancellation occurs during  **pthread\_join**, the  *th* thread remains not joined.

## RETURN VALUE

On success, the return value of  *th* is stored in the location pointed to by  *thread\_return*, and 0 is returned. On error, a non-zero error code is returned.

## ERRORS

- **ESRCH**

  No thread could be found corresponding to that specified by  *th*.

- **EINVAL**

  The  *th* thread has been detached.

- **EINVAL**

  Another thread is already waiting on termination of  *th*.

- **EDEADLK**

  The  *th* argument refers to the calling thread.

## AUTHOR

Xavier Leroy \<Xavier.Leroy@inria.fr\>

## SEE ALSO

[**pthread\_exit**(3)](/pthread_exit/3.md), [**pthread\_detach**(3)](/pthread_detach/3.md), [**pthread\_create**(3)](/pthread_create/3.md), [**pthread\_attr\_setdetachstate**(3)](/pthread_attr_setdetachstate/3.md), [**pthread\_cleanup\_push**(3)](/pthread_cleanup_push/3.md), [**pthread\_key\_create**(3)](/pthread_key_create/3.md).
