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 the attr argument to pthread_attr_getdetachstate() or pthread_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 of detachstate was 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> πŸ“š


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 πŸ’–βœ¨