pthread_attr_affinity_np(3)

pthread_attr_affinity_np3

Manage cpu affinity in thread attribute objects

Usage

#include <pthread_np.h>

int

pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize, cpuset_t *cpusetp);

int

pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize, const cpuset_t *cpusetp);

Library

POSIX Threads Library (libpthread, -lpthread)

Overview

The pthread_attr_getaffinity_np() and pthread_attr_setaffinity_np() functions allow the manipulation of sets of CPUs available to the specified thread attribute object.

Masks of type cpuset_t are composed using the CPU_SET macros. If the user-supplied mask is not large enough to fit all of the matching CPUs, pthread_attr_getaffinity_np() fails with ERANGE. Calls to pthread_attr_setaffinity_np() tolerate masks of any size with no restrictions. pthread_attr_setaffinity_np() uses the meaningful part of the mask, where the upper bound is the maximum CPU id present in the system. If bits for non-existing CPUs are set, calls to pthread_attr_setaffinity_np() fail with EINVAL.

The supplied mask should have a size of cpusetsize bytes. This size is usually provided by calling sizeof(cpuset_t) which is ultimately determined by the value of CPU_SETSIZE as defined in ⟨sys/cpuset.h⟩.

pthread_attr_getaffinity_np() retrieves the mask from the thread attribute object specified by pattr, and stores it in the space provided by cpusetp.

pthread_attr_setaffinity_np() sets the mask for the thread attribute object specified by pattr to the value in cpusetp.

Return values

If successful, the pthread_attr_getaffinity_np() and pthread_attr_setaffinity_np() functions will return zero. Otherwise an error number will be returned to indicate the error.

Errors

The pthread_attr_getaffinity_np() functions will fail if:

[EINVAL]

The pattr or the attribute specified by it is NULL.

[ERANGE]

The cpusetsize is too small.

The pthread_attr_setaffinity_np() function will fail if:

[EINVAL]

The pattr or the attribute specified by it is NULL.

[EINVAL]

The cpusetp specified a CPU that was outside the set supported by the kernel.

[ENOMEM]

Insufficient memory exists to store the cpuset mask.

Releases

40 releases since FreeBSD 7.3-RELEASE; the text changed 29 times.

  1. 7.3
  2. 7.4
  3. 8.1
  4. 8.2
  5. 8.3
  6. 8.4
  7. 9.0
  8. 9.1
  9. 9.2
  10. 9.3
  11. 10.0
  12. 10.1
  13. 10.2
  14. 10.3
  15. 10.4
  16. 11.0
  17. 11.1
  18. 11.2
  19. 11.3
  20. 11.4
  21. 12.0
  22. 12.1
  23. 12.2
  24. 12.3
  25. 12.4
  26. 13.0
  27. 13.1
  28. 13.2
  29. 13.3–13.5
  30. 14.0–16.0

Standards

The pthread_attr_getaffinity_np and pthread_attr_setaffinity_np functions are non-standard FreeBSD extensions and may be not available on other operating systems.

History

The pthread_attr_getaffinity_np and pthread_attr_setaffinity_np functions first appeared in FreeBSD 7.2.

Authors

The pthread_attr_getaffinity_np and pthread_attr_setaffinity_np functions were written by David Xu ⟨[email protected]⟩, and this manpage was written by Xin LI ⟨[email protected]⟩.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.