2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_setaffinity_np 3 (date) "Linux man-pages (unreleased)"
9 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
10 CPU affinity of a thread
13 .RI ( libpthread ", " \-lpthread )
16 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
17 .B #include <pthread.h>
19 .BI "int pthread_setaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
20 .BI " const cpu_set_t *" cpuset );
21 .BI "int pthread_getaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
22 .BI " cpu_set_t *" cpuset );
26 .BR pthread_setaffinity_np ()
28 sets the CPU affinity mask of the thread
30 to the CPU set pointed to by
32 If the call is successful,
33 and the thread is not currently running on one of the CPUs in
35 then it is migrated to one of those CPUs.
38 .BR pthread_getaffinity_np ()
39 function returns the CPU affinity mask of the thread
41 in the buffer pointed to by
44 For more details on CPU affinity masks, see
45 .BR sched_setaffinity (2).
46 For a description of a set of macros
47 that can be used to manipulate and inspect CPU sets, see
52 is the length (in bytes) of the buffer pointed to by
54 Typically, this argument would be specified as
55 .IR sizeof(cpu_set_t) .
56 (It may be some other value, if using the macros described in
58 for dynamically allocating a CPU set.)
60 On success, these functions return 0;
61 on error, they return a nonzero error number.
65 A supplied memory address was invalid.
68 .RB ( pthread_setaffinity_np ())
71 contains no processors that are currently physically on the system
72 and permitted to the thread according to any restrictions that
73 may be imposed by the "cpuset" mechanism described in
77 .RB ( pthread_setaffinity_np ())
79 specified a CPU that was outside the set supported by the kernel.
80 (The kernel configuration option
82 defines the range of the set supported by the kernel data type
84 used to represent CPU sets.)
85 .\" The raw sched_getaffinity() system call returns the size (in bytes)
86 .\" of the cpumask_t type.
89 .RB ( pthread_getaffinity_np ())
91 is smaller than the size of the affinity mask used by the kernel.
98 For an explanation of the terms used in this section, see
104 Interface Attribute Value
108 .BR pthread_setaffinity_np (),
109 .BR pthread_getaffinity_np ()
110 T} Thread safety MT-Safe
114 hence the suffix "_np" (nonportable) in the names.
119 versions of these functions were provided that did not have a
122 Instead the CPU set size given to the underlying system calls was always
123 .IR sizeof(cpu_set_t) .
126 .BR pthread_setaffinity_np (),
127 the set of CPUs on which the thread will actually run is
128 the intersection of the set specified in the
130 argument and the set of CPUs actually present on the system.
131 The system may further restrict the set of CPUs on which the thread
132 runs if the "cpuset" mechanism described in
135 These restrictions on the actual set of CPUs on which the thread
136 will run are silently imposed by the kernel.
138 These functions are implemented on top of the
139 .BR sched_setaffinity (2)
141 .BR sched_getaffinity (2)
144 A new thread created by
145 .BR pthread_create (3)
146 inherits a copy of its creator's CPU affinity mask.
148 In the following program, the main thread uses
149 .BR pthread_setaffinity_np ()
150 to set its CPU affinity mask to include CPUs 0 to 7
151 (which may not all be available on the system),
153 .BR pthread_getaffinity_np ()
154 to check the resulting CPU affinity mask of the thread.
156 .\" SRC BEGIN (pthread_setaffinity_np.c)
172 thread = pthread_self();
174 /* Set affinity mask to include CPUs 0 to 7. */
177 for (size_t j = 0; j < 8; j++)
180 s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
182 errc(EXIT_FAILURE, s, "pthread_setaffinity_np");
184 /* Check the actual affinity mask assigned to the thread. */
186 s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
188 errc(EXIT_FAILURE, s, "pthread_getaffinity_np");
190 printf("Set returned by pthread_getaffinity_np() contained:\en");
191 for (size_t j = 0; j < CPU_SETSIZE; j++)
192 if (CPU_ISSET(j, &cpuset))
193 printf(" CPU %zu\en", j);
200 .BR sched_setaffinity (2),
202 .BR pthread_attr_setaffinity_np (3),
203 .BR pthread_self (3),
204 .BR sched_getcpu (3),