[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


pthread_attr_setdetachstate

Changes the detachstate attribute in the specified thread attributes object.

Syntax

pthread_attr_setdetachstate(
                            attr,
                            detachstate);
 


Argument Data Type Access

attr opaque pthread_attr_t write detachstate integer read


C Binding

#include 

int pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate);

Arguments

attr
Thread attributes object to be modified.
detachstate
New value for the detachstate attribute. Valid values are as follows:
PTHREAD_CREATE_JOINABLE  This is the default value. Threads are
                         created in "undetached" state.

PTHREAD_CREATE_DETACHED  The created thread is detached immediately,
                         before it begins running.


Description

This routine changes the detachstate attribute in the thread creation attributes. This attribute specifies whether the threads created using the specified thread attributes object are created in a detached state or not. A value of PTHREAD_CREATE_JOINABLE indicates the thread is not detached, and a value of PTHREAD_CREATE_ DETACHED indicates the thread is detached. PTHREAD_CREATE_JOINABLE is the default value.

You cannot use the thread handle (the value of type pthread_t that is returned by pthread_create) for a detached thread. This means that you cannot cancel the thread with pthread_cancel. You also cannot use pthread_join to wait for the thread to complete or to retrieve the thread's return status.

When a thread that has not been detached completes execution, DECthreads will retain the state of that thread to allow another thread to join with it. If the thread is detached before it completes, DECthreads is free to immediately reclaim the thread's storage and resources. Failing to detach threads that have completed execution can result in wasting resources, so threads should be detached as soon as the program is done with them. If there is no need to use the thread's handle after creation, create the thread initially detached.

Return Values

If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows:
Return        Description

0 Successful completion. [EINVAL] The value specified by the detachstate argument is invalid.


Associated Routines

   pthread_attr_init
   pthread_attr_getdetachstate
   pthread_create
   pthread_join



[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]