pthread_attr_setdetachstate()--Set Thread Attributes Object Detachstate


  Syntax:
 #include <pthread.h>
 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_attr_setdetachstate() function sets the detach state of the thread attributes object. The detach state of a thread indicates whether the system is allowed to free thread resources (including but not limited to thread exit status) when the thread terminates. Some resources (like automatic storage) are always freed when a thread ends.

The detach state specifies one of PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE. The default detach state (DEFAULT_DETACHSTATE) is PTHREAD_CREATE_JOINABLE.


Authorities and Locks

None.


Parameters

attr
(Input) The address of the thread attributes object.

detachstate
(Output) The detach state, one of PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.

Return Value

0
pthread_attr_setdetachstate() was successful.
value
pthread_attr_setdetachstate() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_attr_setdetachstate() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

void showDetachState(pthread_attr_t *a)
{
  int   rc=0;
  int   state=0;

  printf("Get detach state\n");
  rc = pthread_attr_getdetachstate(a, &state);
  checkResults("pthread_attr_getdetachstate()\n", rc);

  printf("The thread attributes object indicates: ");
  switch (state) {
  case PTHREAD_CREATE_DETACHED:
    printf("DETACHED\n");
    break;
  case PTHREAD_CREATE_JOINABLE:
    printf("JOINABLE\n");
    break;
  }
  return;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create a default thread attributes object\n");
  rc = pthread_attr_init(&pta);

  checkResults("pthread_attr_init()\n", rc);

  showDetachState(&pta);

  printf("Set the detach state\n");
  rc = pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_DETACHED);
  checkResults("pthread_attr_setdetachstate()\n", rc);

  showDetachState(&pta);

  printf("Destroy thread attributes object\n");
  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()\n", rc);

  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TASDS0
Create a default thread attributes object
Get detach state
The thread attributes object indicates: JOINABLE
Set the detach state
Get detach state
The thread attributes object indicates: DETACHED
Destroy thread attributes object
Main completed

API introduced: V4R3
Top | Pthread APIs | APIs by category