pthread_attr_setscope(3)
β¨ pthread_attr_setscope(3) β¨
okay bestie, so this one lives in the Library Functions Manual section of the man pages, and it's literally about the contention scope attribute on a thread attributes object. get comfy, we're doing this π
πΈ NAME πΈ
pthread_attr_setscope, pthread_attr_getscope β set/get the contention scope attribute in a thread attributes object. one sets, one gets, they're a lil duo π«Ά
π LIBRARY π
POSIX threads library β that's libpthread, and you link it with -lpthread. classic.
π SYNOPSIS π
#include <pthread.h>
int pthread_attr_setscope(pthread_attr_t *attr, int scope);
int pthread_attr_getscope(const pthread_attr_t *restrict attr,
int *restrict scope);
π DESCRIPTION π
so. pthread_attr_setscope() sets the contention scope attribute of the thread attributes object that attr points to, and it sets it to whatever value you put in scope. and what even is contention scope, you ask? it's the set of threads that your thread is competing against for resources β think CPU time, the whole thing. the girlies are fighting for the scheduler's attention, and the scope decides who's in the ring with you π₯
POSIX.1 says there are two possible values for scope:
PTHREAD_SCOPE_SYSTEMβ your thread competes for resources with all other threads in all processes on the system that are in the same scheduling allocation domain (that's a group of one or more processors).PTHREAD_SCOPE_SYSTEMthreads get scheduled relative to one another according to their scheduling policy and priority. main character energy fr, you're competing with everybody.PTHREAD_SCOPE_PROCESSβ your thread competes for resources with all the other threads in the same process that were also created with thePTHREAD_SCOPE_PROCESScontention scope.PTHREAD_SCOPE_PROCESSthreads are scheduled relative to other threads in the process according to their scheduling policy and priority. and here's the thing β POSIX.1 leaves it unspecified how these threads contend with other threads in other processes on the system, or with other threads in the same process that were created with thePTHREAD_SCOPE_SYSTEMcontention scope. so basically, nobody promised you anything there π
POSIX.1 requires that an implementation support at least one of these contention scopes. and on Linux? Linux supports PTHREAD_SCOPE_SYSTEM, but not PTHREAD_SCOPE_PROCESS. no cap.
so like, on systems that do support multiple contention scopes, for the parameter setting from pthread_attr_setscope() to actually have any effect when you call pthread_create(3), you gotta use pthread_attr_setinheritsched(3) to set the inherit-scheduler attribute of the attributes object attr to PTHREAD_EXPLICIT_SCHED. otherwise your lil scope setting is just... vibing there, unnoticed π
and then pthread_attr_getscope() returns the contention scope attribute of the thread attributes object referred to by attr, into the buffer that scope points at. give it somewhere to put the answer, she's not gonna hold it for you π
π₯ RETURN VALUE π₯
success? both functions return 0. error? they return a nonzero error number. that's it, that's the whole deal.
π ERRORS π
pthread_attr_setscope() can fail with these:
EINVALβ an invalid value was specified inscope. girl, that's not a real scope πENOTSUPβscopespecified the valuePTHREAD_SCOPE_PROCESS, which is not supported on Linux. we've been over this, bestie π
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7).
pthread_attr_setscope(),pthread_attr_getscope()β Thread safety:MT-Safeβ
β STANDARDS β
POSIX.1-2008. periodt.
π HISTORY π
POSIX.1-2001. she's been around a minute π
π¦ NOTES π¦
the PTHREAD_SCOPE_SYSTEM contention scope typically indicates that a user-space thread is bound directly to a single kernel-scheduling entity. that's the case on Linux for the obsolete LinuxThreads implementation and for the modern NPTL implementation β both of which are 1:1 threading implementations. iconic lineage honestly π
POSIX.1 specifies that the default contention scope is implementation-defined. so don't come for anybody's default, it's however the implementation woke up feeling today.
π SEE ALSO π
pthread_attr_init(3), pthread_attr_setaffinity_np(3), pthread_attr_setinheritsched(3), pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3), pthread_create(3), pthreads(7)
and that's the whole page, bestie π«Ά big love to the Linux man-pages 6.20 girlies β this one's dated 2025-05-17, and it's pthread_attr_setscope(3) all the way down. mwah πβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms