---
name: "pthread_attr_affinity_np"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/pthread_attr_affinity_np"
---

# pthread\_attr\_affinity\_np(3)

manage CPU affinity in thread attribute objects

## NAME

**pthread\_attr\_getaffinity\_np**, **pthread\_attr\_setaffinity\_np** – manage CPU affinity in thread attribute objects

## LIBRARY

POSIX Threads Library (libpthread, -lpthread)

## SYNOPSIS

**#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*);

## DESCRIPTION

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.

## SEE ALSO

[cpuset(1)](/cpuset/1.md), [cpuset(2)](/cpuset/2.md), [cpuset\_getid(2)](/cpuset_getid/2.md), [cpuset\_setid(2)](/cpuset_setid/2.md), [pthread\_getaffinity\_np(3)](/pthread_getaffinity_np/3.md), [pthread\_np(3)](/pthread_np/3.md), [pthread\_setaffinity\_np(3)](/pthread_setaffinity_np/3.md)

## 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 ⟨*davidxu@FreeBSD.org*⟩, and this manpage was written by Xin LI ⟨*delphij@FreeBSD.org*⟩.
