PTHREAD_A...CHEDPOLICY(3P)

โœจ pthread_attr_getschedpolicy, pthread_attr_setschedpolicy โœจ

okay bestie, so this is a page from the POSIX Programmer's Manual (section 3P, we love a section code), and it's all about getting and setting the schedpolicy attribute. REALTIME THREADS edition, periodt. ๐Ÿงต๐Ÿ‘‘


๐ŸŒธ PROLOG ๐ŸŒธ

okay real talk before we start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so if you're on Linux, go consult the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. ๐Ÿ’€ not me being a POSIX purist about it.


๐Ÿ’… NAME ๐Ÿ’…

pthread_attr_getschedpolicy, pthread_attr_setschedpolicy โ€” get and set the schedpolicy attribute (REALTIME THREADS). yes bestie it's giving "get and set", it's giving duality. ๐Ÿฆ‹


๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

#include <pthread.h>

int pthread_attr_getschedpolicy(const pthread_attr_t *restrict attr,
    int *restrict policy);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

look at those restrict qualifiers, they're so iconic, they're literally promising nobody else is touching that memory. ๐Ÿ’–


๐Ÿ‘‘ DESCRIPTION ๐Ÿ‘‘

so. pthread_attr_getschedpolicy() and pthread_attr_setschedpolicy() shall (POSIX said what it said) respectively get and set the schedpolicy attribute in the attr argument. that's the whole vibe. โœจ

the supported values of policy shall include SCHED_FIFO, SCHED_RR, and SCHED_OTHER, which are defined in the <sched.h> header. ๐Ÿ“Œ

and here's the part that's actually highkey important: when threads executing with the scheduling policy SCHED_FIFO, SCHED_RR, or SCHED_SPORADIC are waiting on a mutex, they shall acquire the mutex in priority order when the mutex is unlocked. priority order!! the VIP line!! ๐Ÿ‘‘

โš ๏ธ okay listen. the behavior is undefined if the value specified by the attr argument to pthread_attr_getschedpolicy() or pthread_attr_setschedpolicy() does not refer to an initialized thread attributes object. undefined behavior is not cute, it's not a slay, it's a "your program is now a haunted house" situation. initialize your attrs, bestie. ๐Ÿซถ

๐ŸŽ€ RETURN VALUE ๐ŸŽ€

if it slays: pthread_attr_getschedpolicy() and pthread_attr_setschedpolicy() shall return zero. zero = the win. ๐Ÿ†

if it doesn't: an error number shall be returned to indicate the error. no cap.


๐Ÿšจ ERRORS ๐Ÿšจ

the pthread_attr_setschedpolicy() function shall fail if:

  • ENOTSUP โ€” an attempt was made to set the attribute to an unsupported value. babe. it's not supported. it said no. ๐Ÿ™ˆ

the pthread_attr_setschedpolicy() function may fail if:

  • EINVAL โ€” the value of policy is not valid. you handed it something it does not recognize. that's not a valid policy, that's a cry for help. ๐Ÿ˜ญ

and then the note: these functions shall not return an error code of [EINTR]. nope, not a thing here. ๐Ÿ’…

oh also: the following sections are informative. iykyk โ€” informative means "flavor text, not normative requirements". ๐Ÿ“–โœจ


๐Ÿ“ EXAMPLES ๐Ÿ“

None.

(i'm not being lazy, that's literally what it says. the manpage said "None." and i respect it for that. ๐Ÿ’€)


๐ŸŒผ APPLICATION USAGE ๐ŸŒผ

after these attributes have been set, a thread can be created with the specified attributes using pthread_create(). ๐Ÿงต

and here's a little โœจ boundary setting โœจ moment: using these routines does not affect the current running thread. the current thread is unbothered. the current thread is thriving. the current thread is not taking questions. ๐Ÿซถ

see Section 2.9.4, Thread Scheduling for further details on thread scheduling attributes and their default settings. go read it, it's giving required reading for the scheduling girlies. ๐Ÿ“š


๐Ÿ’ญ RATIONALE ๐Ÿ’ญ

if an implementation detects that the value specified by the attr argument to pthread_attr_getschedpolicy() or pthread_attr_setschedpolicy() does not refer to an initialized thread attributes object, it is recommended that the function should fail and report an [EINVAL] error. recommended! like, we're suggesting it. we think it would be nice. ๐Ÿฅบ


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None.

(future: still none. the future is stable. we love stability. ๐ŸŒˆ)


๐Ÿ”— SEE ALSO ๐Ÿ”—

  • pthread_attr_destroy()
  • pthread_attr_getscope()
  • pthread_attr_getinheritsched()
  • pthread_attr_getschedparam()
  • pthread_create()

and from the Base Definitions volume of POSIX.1โ€2017: <pthread.h>, <sched.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. (referee = final boss = the one who decides. respect it. ๐Ÿ‘‘) 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. ๐Ÿ›


๐Ÿซถ cute little sign-off ๐Ÿซถ

this whole thing was brought to you by IEEE/The Open Group, 2017, filed under PTHREAD_A...CHEDPOLICY(3P) โ€” yeah the title got truncated in the middle, that's just how it is, the manpage said "a...chedpolicy" and honestly? iconic. ๐Ÿ’œโœจ

go forth and set your schedpolicy responsibly. mwah. ๐Ÿ’…๐ŸŽ€

๐Ÿ“ฆ where this page comes from