Multithreaded Programming Guide

Detaching a Thread

pthread_detach(3C) is an alternative to pthread_join(3C) to reclaim storage for a thread that is created with a detachstate attribute set to PTHREAD_CREATE_JOINABLE.

pthread_detach Syntax

int pthread_detach(pthread_t tid);
#include <pthread.h>

pthread_t tid;
int ret;

/* detach thread tid */
ret = pthread_detach(tid); 

The pthread_detach() function is used to indicate to your application that storage for the thread tid can be reclaimed when the thread terminates. Threads should be detached when they are no longer needed. If tid has not terminated, pthread_detach() does not cause the thread to terminate.

pthread_detach Return Values

pthread_detach() returns zero when the call completes successfully. Any other return value indicates that an error occurred. When any of the following conditions is detected, pthread_detach() fails and returns the corresponding value.



tid is a detached thread.



tid is not a valid, undetached thread in the current process.