PTHREAD_SETSCHEDPRIO(3P)

🌸 PTHREAD_SETSCHEDPRIO(3P) β€” POSIX Programmer's Manual 🌸

okay bestie, before we even start β€” this page lives in the POSIX Programmer's Manual πŸ’–, and real talk: the Linux implementation of this interface may be different (so go check the matching Linux manual page for the deets on Linux behavior), or honestly it might not even be implemented on Linux at all. no cap. πŸ™ˆ


✨ NAME ✨

pthread_setschedprio β€” dynamic thread scheduling parameters access (REALTIME THREADS) πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <pthread.h>

int pthread_setschedprio(pthread_t thread, int prio);

πŸ’… DESCRIPTION πŸ’…

so the way that pthread_setschedprio() works is: it shall set the scheduling priority for the thread whose thread ID is given by thread to the value given by prio. literally just vibes-to-priority pipeline. ✨

wanna know how this call messes with the ordering of the thread in the thread list for its new priority? go read Scheduling Policies β€” it's all there, bestie.

🫢 lowkey important: if pthread_setschedprio() fails, the scheduling priority of the target thread shall not be changed. so either it works or nothing happens, no half-finished glow-up. πŸ’€

πŸ‘‘ RETURN VALUE πŸ‘‘

if it slays: pthread_setschedprio() shall return zero. 🌈

otherwise it returns an error number to indicate the error. not the vibe, but at least it tells you. 😭


πŸ”₯ ERRORS πŸ”₯

pthread_setschedprio() may fail if:

  • EINVAL β€” the value of prio is invalid for the scheduling policy of the specified thread. 🚫
  • EPERM β€” the caller does not have appropriate privileges to set the scheduling priority of the specified thread. no permission, no priority, periodt. πŸ’…

also!! pthread_setschedprio() shall not return an error code of [EINTR]. noted. ⭐

the following sections are informative (aka the fun bonus lore πŸ“–).


πŸŽ€ EXAMPLES πŸŽ€

None. (yes bestie, truly none, we're all just built different 🧚)


πŸ“ APPLICATION USAGE πŸ“

None. (again!! iconic minimalism πŸ’œ)


🌸 RATIONALE 🌸

okay so here's the why, and it's actually kind of genius:

pthread_setschedprio() gives an application a way to temporarily raise its priority and then lower it again β€” without the undesired side-effect of yielding to other threads of the same priority. πŸ™…β€β™€οΈ

this is necessary if the application is gonna implement its own strategies for bounding priority inversion, like priority inheritance or priority ceilings. this capability is especially important if the implementation does not support the Thread Priority Protection or Thread Priority Inheritance options β€” but even if those options are supported, it's still needed if the application is gonna bound priority inheritance for other resources, such as semaphores. 🧠✨

and then the plot twist: the standard developers considered that while it might be conceptually preferable to solve this problem by modifying the spec of pthread_setschedparam(), it was too late to make such a change β€” because there may be implementations that would need to be changed. so instead, this whole new function was introduced. slay, pivot. πŸ‘‘

also: if an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error. ⚠️


πŸ¦„ FUTURE DIRECTIONS πŸ¦„

None. (we're at peace with the present 🫢)


⭐ SEE ALSO ⭐

  • Scheduling Policies
  • pthread_getschedparam()

and from 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!! πŸŽ‰ brought to you by IEEE/The Open Group, 2017, and it's literally PTHREAD_SETSCHEDPRIO(3P) β€” go forth and set those priorities, you main character. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from