PTHREAD...ACHSTATE(3POSIX)
π PTHREAD_ATTR_GETDETACHSTATE, PTHREAD_ATTR_SETDETACHSTATE β get and set the detachstate attribute π
okay bestie, welcome to the POSIX Programmer's Manual, section 3POSIX π«Ά this one's all about the detachstate attribute and honestly? it's giving "small but mighty". let's get into it β¨
πΈ PROLOG πΈ
okay so first things first, bestie, this manual page is part of the POSIX Programmer's Manual π and here's the tea: the Linux implementation of this interface may differ (so pls go consult the corresponding Linux manual page for the deets on Linux behavior), or the interface might just... not be implemented on Linux at all π. vibe check your platform, okay?
β¨ NAME β¨
pthread_attr_getdetachstate, pthread_attr_setdetachstate β get and set the detachstate attribute
π SYNOPSIS π
#include <pthread.h>
int pthread_attr_getdetachstate(const pthread_attr_t *attr,
int *detachstate);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
π DESCRIPTION π
so the detachstate attribute is literally the thing that controls whether your thread gets created in a detached state. and listen, if the thread is created detached, then using the ID of that newly created thread with pthread_detach() or pthread_join() is an error π. no take-backs, no joining, it's on its own main character journey.
the pthread_attr_getdetachstate() and pthread_attr_setdetachstate() functions, respectively, shall get and set the detachstate attribute in the attr object.
for pthread_attr_getdetachstate(), detachstate shall be set to either PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE.
for pthread_attr_setdetachstate(), the application shall set detachstate to either PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE.
a value of PTHREAD_CREATE_DETACHED shall cause all threads created with attr to be in the detached state, whereas using a value of PTHREAD_CREATE_JOINABLE shall cause all threads created with attr to be in the joinable state. and the default value of the detachstate attribute shall be PTHREAD_CREATE_JOINABLE π
(yes, joinable is the default, that's the standard behavior, periodt).
β οΈ bestie this is important: the behavior is undefined if the value specified by theattrargument topthread_attr_getdetachstate()orpthread_attr_setdetachstate()does not refer to an initialized thread attributes object. undefined means undefined, no cap. initialize your attr object first π«Ά
π RETURN VALUE π
upon successful completion, pthread_attr_getdetachstate() and pthread_attr_setdetachstate() shall return a value of 0; otherwise, an error number shall be returned to indicate the error.
and for pthread_attr_getdetachstate() specifically: it stores the value of the detachstate attribute in detachstate if successful β¨
π ERRORS π
the pthread_attr_setdetachstate() function shall fail if:
EINVALβ the value ofdetachstatewas not valid
these functions shall not return an error code of [EINTR]. not today, signals π
ββοΈ
the following sections are informative.
π¦ EXAMPLES π¦
πΈ Retrieving the detachstate Attribute πΈ
this example shows how to obtain the detachstate attribute of a thread attribute object. iconic behavior, honestly π
#include <pthread.h>
pthread_attr_t thread_attr;
int detachstate;
int rc;
/* code initializing thread_attr */
...
rc = pthread_attr_getdetachstate (&thread_attr, &detachstate);
if (rc!=0) {
/* handle error */
...
}
else {
/* legal values for detachstate are:
* PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE
*/
...
}
π§ APPLICATION USAGE π§
None. (nothing to add bestie, it's giving minimal β¨)
π RATIONALE π
if an implementation detects that the value specified by the attr argument to pthread_attr_getdetachstate() or pthread_attr_setdetachstate() does not refer to an initialized thread attributes object, it is recommended that the function should fail and report an [EINVAL] error. like, it's not mandatory-mandatory, but it's the recommended serve π
β FUTURE DIRECTIONS β
None. (the future is... the same, bestie π¦)
π₯ SEE ALSO π₯
pthread_attr_destroy()pthread_attr_getstacksize()pthread_create()
the Base Definitions volume of POSIX.1β2017, <pthread.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! π this was the POSIX Programmer's Manual, IEEE/The Open Group, 2017, section 3POSIX β go forth and detach (or not) responsibly πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms