Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
int pthread_attr_getdetachstate(pthread_attr_t *attr);
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_attr_getdetachstate(const pthread_attr_t *attr,
int *detachstate);
You can use a thread attribute object to manage the characteristics of threads in your application. It defines the set of values to be used for the thread during its creation. By establishing a thread attribute object, you can create many threads with the same set of characteristics, without defining those characteristics for each thread. You can define more than one thread attribute object.
If successful, pthread_attr_getdetachstate() returns the detachstate (0 or 1).
If unsuccessful, pthread_attr_getdetachstate() returns -1.
There are no documented errno values. Use perror() or strerror() to determine the cause of the error.
Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_attr_getdetachstate() returns an error number to indicate the error.
CELEBP07
⁄* CELEBP07 *⁄
#define _OPEN_THREADS
#include <stdio.h>
#include <pthread.h>
int main()
{
pthread_attr_t attr;
char typ[12];
if (pthread_attr_init(&attr) == -1) {
perror("error in pthread_attr_init");
exit(1);
}
switch(pthread_attr_getdetachstate(&attr)) {
default:
perror("error in pthread_attr_getdetachstate()");
exit(2);
case 0:
strcpy(typ, "undetached");
break;
case 1:
strcpy(typ, "detached");
}
printf("The detach state is %s.\n", typ);
if (pthread_attr_destroy(&attr) == -1) {
perror("error in pthread_attr_destroy");
exit(2);
}
exit(0);
}
CELEBP61
⁄* CELEBP61 *⁄
⁄* Example using SUSv3 pthread_attr_getdetachstate() interface *⁄
#define _UNIX03_THREADS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
int main(void)
{
pthread_attr_t attr;
int rc, newstate, foundstate;
char state[12];
if (pthread_attr_init(&attr) == -1) {
perror("error in pthread_attr_init");
exit(1);
}
newstate = PTHREAD_CREATE_DETACHED;
pthread_attr_setdetachstate(&attr, newstate);
rc = pthread_attr_getdetachstate(&attr,&foundstate);
switch(foundstate) {
case PTHREAD_CREATE_JOINABLE:
strcpy(state,"joinable");
break;
case PTHREAD_CREATE_DETACHED:
strcpy(state,"detached");
break;
default:
printf("pthread_attr_getdetachstate returned: %d\n", rc);
printf("Error: %d, Errno_Jr: %08x\n", errno, __errno2());
exit(2);
}
printf("Threads created with this attribute object are %s.\n",state);
if (pthread_attr_destroy(&attr) == -1) {
perror("error in pthread_attr_destroy");
exit(3);
}
exit(0);
}