[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Base Operating System and Extensions Technical Reference, Volume 1

pthread_join, or pthread_detach Subroutine

Purpose

Blocks the calling thread until the specified thread terminates.

Library

Threads Library (libpthreads.a)

Syntax

#include <pthread.h>
int pthread_join (pthread_t thread, void 
**value_ptr);
int pthread_detach (pthread_t thread;
**value_ptr);

Description

The pthread_join subroutine blocks the calling thread until the thread thread terminates. The target thread's termination status is returned in the status parameter.

If the target thread is already terminated, but not yet detached, the subroutine returns immediately. It is impossible to join a detached thread, even if it is not yet terminated. The target thread is automatically detached after all joined threads have been woken up.

This subroutine does not itself cause a thread to be terminated. It acts like the pthread_cond_wait subroutine to wait for a special condition.

Note: The pthread.h header file must be the first included file of each source file using the threads library. Otherwise, the -D_THREAD_SAFE compilation flag should be used, or the cc_r compiler used. In this case, the flag is automatically set.

The pthread_detach subroutine is used to indicate to the implementation that storage for the thread whose thread ID is in the location thread can be reclaimed when that thread terminates. This storage shall be reclaimed on process exit, regardless of whether the thread has been detached or not, and may include storage for thread return value. If thread has not yet terminated, pthread_detach shall not cause it to terminate. Multiple pthread_detach calls on the same target thread causes an error.

Parameters

thread Specifies the target thread.
status Points to where the termination status of the target thread will be stored. If the value is NULL, the termination status is not returned.

Return Values

If successful, the pthread_join function returns zero. Otherwise, an error number is returned to indicate the error.

Error Codes

The pthread_join and pthread_detach functions will fail if:

EINVAL The implementation has detected that the value specified by thread does not refer to a joinable thread.
ESRCH No thread could be found corresponding to that specified by the given thread ID.

The pthread_join function will fail if:

EDEADLK The value of thread specifies the calling thread.

The pthread_join function will not return an error code of EINTR.

Implementation Specifics

This subroutine is part of the Base Operating System (BOS) Runtime.

Related Information

The pthread_exit subroutine, pthread_create subroutine, wait subroutine, pthread_cond_wait or pthread_cond_timedwait subroutines, the pthread.h file.

Joining Threads in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.

Threads Library Quick Reference in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]