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_SYSTEM threads 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 the PTHREAD_SCOPE_PROCESS contention scope. PTHREAD_SCOPE_PROCESS threads 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 the PTHREAD_SCOPE_SYSTEM contention 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 in scope. girl, that's not a real scope 😭
  • ENOTSUP β€” scope specified the value PTHREAD_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