PTHREAD_ATTR_SETAFFINITY_NP(3)

πŸ’– pthread_at...affinity_np(3) β€” Library Functions Manual πŸ’–

okay bestie, come sit with me, we're doing CPU affinities today and honestly? it's giving control freak in the best way. this is pthread_at...affinity_np(3), straight out of the Library Functions Manual, and it's all about telling your threads exactly which CPUs they're allowed to vibe on. πŸ‘‘


✨ NAME ✨

pthread_attr_setaffinity_np, pthread_attr_getaffinity_np β€” set/get the CPU affinity attribute in a thread attributes object. one sets it, one gets it, they're a matching set, they ate. πŸ’…


πŸ“š LIBRARY πŸ“š

POSIX threads library β€” that's libpthread, link it with -lpthread. no cap. 🧡


πŸŽ€ SYNOPSIS πŸŽ€

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <pthread.h>

int pthread_attr_setaffinity_np(pthread_attr_t *attr,
                   size_t cpusetsize, const cpu_set_t *cpuset);
int pthread_attr_getaffinity_np(const pthread_attr_t *attr,
                   size_t cpusetsize, cpu_set_t *cpuset);

like, yes you need _GNU_SOURCE first, it's nonportable, we'll get to that. πŸ™ˆ


🌸 DESCRIPTION 🌸

so pthread_attr_setaffinity_np() sets the CPU affinity mask attribute of the thread attributes object pointed at by attr to whatever you put in cpuset. and this attribute is literally the thing that determines the CPU affinity mask of a thread that gets created using that attributes object attr. so you're setting the vibe before the thread is even born. main character preplanning. ✨

pthread_attr_getaffinity_np() does the reverse β€” it hands you back the CPU affinity mask attribute of the thread attributes object referred to by attr, and it drops it into the buffer that cpuset points to. 🫢

the argument cpusetsize is the length in bytes of the buffer that cpuset points to. and typically? you just say sizeof(cpu_set_t). that's the move, that's always the move. πŸ’–

want the full lore on CPU affinity masks? go read sched_setaffinity(2). want the rundown on the macro collection you use to poke and prod CPU sets? that's CPU_SET(3). iykyk. πŸ¦‹


πŸ’€ RETURN VALUE πŸ’€

on success these functions return 0. on error they return a nonzero error number. that's it, that's the whole deal. no errno drama here, bestie, the error number is the return value. 😭


πŸ”₯ ERRORS πŸ”₯

  • EINVAL (pthread_attr_setaffinity_np()) β€” the cpuset you passed named a CPU that's outside the set the kernel actually supports. (fun fact: the kernel config option CONFIG_NR_CPUS defines the range of that set, as supported by the kernel data type used to represent CPU sets.) so like, you tried to point at a CPU that doesn't exist. bold of you. πŸ™ˆ
  • EINVAL (pthread_attr_getaffinity_np()) β€” a CPU inside the affinity mask of the thread attributes object referred to by attr lies outside the range specified by cpusetsize. translation: cpuset/cpusetsize is too small. your buffer is not big enough for this much energy. πŸ’€
  • ENOMEM (pthread_attr_setaffinity_np()) β€” could not allocate memory. it tried, it really did. πŸ₯²

🌈 ATTRIBUTES 🌈

for an explanation of the terms used in this section, go peek at attributes(7).

here's the vibe check, and yes there is only one row:

  • Interface: pthread_attr_setaffinity_np(), pthread_attr_getaffinity_np()
    • Attribute: Thread safety
    • Value: MT-Safe

both functions, one row, MT-Safe, periodt. πŸ’…


⭐ STANDARDS ⭐

GNU. hence the _np suffix in the names β€” and yes _np stands for nonportable, so don't go expecting this to work on your friend's non-glibc system. πŸ‘‘


πŸ•°οΈ HISTORY πŸ•°οΈ

glibc 2.3.4. old money. πŸŽ€


🧚 NOTES 🧚

in glibc 2.3.3 only, there were versions of these functions that had no cpusetsize argument. instead, the CPU set size handed to the underlying system calls was always sizeof(cpu_set_t). that's it, that's the whole note, one specific patch release being quirky. πŸ“


πŸ’œ SEE ALSO πŸ’œ


and that's the page, bestie! this lil manual came to you courtesy of Linux man-pages 6.20, dated 2025-05-17, and it lives at the bottom of the page as pthread_at...affinity_np(3) β€” go forth and pin your threads to exactly the CPUs they deserve. slay. πŸ’–βœ¨πŸ¦‹

πŸ“¦ 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