PTHREAD_SETAFFINITY_NP(3) manual page

#### POSIX Threads for Windows – REFERENCE - Pthreads4W

Reference Index

Table of Contents

## Name

pthread_setaffinity_np - set thread CPU affinity

pthread_getaffinity_np - get thread CPU affinity

## Synopsis

#include <pthread.h>

int pthread_setaffinity_np(pthread_t tid, int cpusetsize, const cpu_set_t *mask);

int pthread_getaffinity_np(pthread_t tid, int cpusetsize, cpu_set_t *mask);

## Description

pthread_setaffinity_np sets the CPU affinity mask of the thread whose ID is tid to the value specified by mask. The argument cpusetsize is the length (in bytes) of the data pointed to by mask. Normally this argument would be specified as sizeof(cpu_set_t).

If the thread specified by tid is not currently running on one of the CPUs specified in mask, then that thread is migrated to one of the CPUs specified in mask.

After a call to pthread_setaffinity_np, the set of CPUs on which the thread will actually run is the intersection of the set specified in the mask argument and the set of CPUs actually present on the system.

pthread_getaffinity_np writes the affinity mask of the thread whose ID is tid into the cpu_set_t structure pointed to by mask. The cpusetsize argument specifies the size (in bytes) of mask.

PThreads4W currently ignores the cpusetsize parameter for either function because cpu_set_t is a direct typeset to the Windows affinity vector type DWORD_PTR.

## Return Value

On success, pthread_setaffinity_np and pthread_getaffinity_np return 0. On error, an error number is returned.

## Errors

EFAULT

A supplied memory address was invalid.

EINVAL

The affinity bit mask mask contains no processors that are currently physically on the system.

EAGAIN

The function did not succeed in changing or obtaining the CPU affinity for some undetermined reason. Try again.

EPERM

The calling process does not have appropriate privileges.

ESRCH

The thread whose ID is tid could not be found.

## Application Usage

A thread's CPU affinity mask determines the set of CPUs on which it is eligible to run. On a multiprocessor system, setting the CPU affinity mask can be used to obtain performance benefits. For example, by dedicating one CPU to a particular thread (i.e., setting the affinity mask of that thread to specify a single CPU, and setting the affinity mask of all other threads to exclude that CPU), it is possible to ensure maximum execution speed for that thread. Restricting a thread to run on a single CPU also minimises the performance cost caused by the cache invalidation that occurs when a thread ceases to execute on one CPU and then recommences execution on a different CPU.

A CPU affinity mask is represented by the cpu_set_t structure, a "CPU set", pointed to by mask. A set of macros for manipulating CPU sets is described in cpu_set(3).

## See Also

cpu_set(3), sched_setaffininty(3), sched_getaffinity(3)

## Copyright

Most of this is taken from the Linux manual page.

Modified by Ross Johnson for use with PThreads4W.

Table of Contents

- Name

- Synopsis

- Description

- Return Value

- Errors

- Application Usage

- See Also

- Copyright

####