1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_SETAFFINITY_NP 3 2021-03-22 "Linux man-pages (unreleased)"
8 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
9 CPU affinity of a thread
12 .RI ( libpthread ", " \-lpthread )
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .B #include <pthread.h>
18 .BI "int pthread_setaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
19 .BI " const cpu_set_t *" cpuset );
20 .BI "int pthread_getaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
21 .BI " cpu_set_t *" cpuset );
25 .BR pthread_setaffinity_np ()
27 sets the CPU affinity mask of the thread
29 to the CPU set pointed to by
31 If the call is successful,
32 and the thread is not currently running on one of the CPUs in
34 then it is migrated to one of those CPUs.
37 .BR pthread_getaffinity_np ()
38 function returns the CPU affinity mask of the thread
40 in the buffer pointed to by
43 For more details on CPU affinity masks, see
44 .BR sched_setaffinity (2).
45 For a description of a set of macros
46 that can be used to manipulate and inspect CPU sets, see
51 is the length (in bytes) of the buffer pointed to by
53 Typically, this argument would be specified as
54 .IR sizeof(cpu_set_t) .
55 (It may be some other value, if using the macros described in
57 for dynamically allocating a CPU set.)
59 On success, these functions return 0;
60 on error, they return a nonzero error number.
64 A supplied memory address was invalid.
67 .RB ( pthread_setaffinity_np ())
70 contains no processors that are currently physically on the system
71 and permitted to the thread according to any restrictions that
72 may be imposed by the "cpuset" mechanism described in
76 .RB ( pthread_setaffinity_np ())
78 specified a CPU that was outside the set supported by the kernel.
79 (The kernel configuration option
81 defines the range of the set supported by the kernel data type
83 used to represent CPU sets.)
84 .\" The raw sched_getaffinity() system call returns the size (in bytes)
85 .\" of the cpumask_t type.
88 .RB ( pthread_getaffinity_np ())
90 is smaller than the size of the affinity mask used by the kernel.
97 These functions are provided by glibc since version 2.3.4.
99 For an explanation of the terms used in this section, see
107 Interface Attribute Value
109 .BR pthread_setaffinity_np (),
110 .BR pthread_getaffinity_np ()
111 T} Thread safety MT-Safe
117 These functions are nonstandard GNU extensions;
118 hence the suffix "_np" (nonportable) in the names.
121 .BR pthread_setaffinity_np (),
122 the set of CPUs on which the thread will actually run is
123 the intersection of the set specified in the
125 argument and the set of CPUs actually present on the system.
126 The system may further restrict the set of CPUs on which the thread
127 runs if the "cpuset" mechanism described in
130 These restrictions on the actual set of CPUs on which the thread
131 will run are silently imposed by the kernel.
133 These functions are implemented on top of the
134 .BR sched_setaffinity (2)
136 .BR sched_getaffinity (2)
140 versions of these functions were provided that did not have a
143 Instead the CPU set size given to the underlying system calls was always
144 .IR sizeof(cpu_set_t) .
146 A new thread created by
147 .BR pthread_create (3)
148 inherits a copy of its creator's CPU affinity mask.
150 In the following program, the main thread uses
151 .BR pthread_setaffinity_np ()
152 to set its CPU affinity mask to include CPUs 0 to 7
153 (which may not all be available on the system),
155 .BR pthread_getaffinity_np ()
156 to check the resulting CPU affinity mask of the thread.
165 #define handle_error_en(en, msg) \e
166 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
169 main(int argc, char *argv[])
175 thread = pthread_self();
177 /* Set affinity mask to include CPUs 0 to 7. */
180 for (int j = 0; j < 8; j++)
183 s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
185 handle_error_en(s, "pthread_setaffinity_np");
187 /* Check the actual affinity mask assigned to the thread. */
189 s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
191 handle_error_en(s, "pthread_getaffinity_np");
193 printf("Set returned by pthread_getaffinity_np() contained:\en");
194 for (int j = 0; j < CPU_SETSIZE; j++)
195 if (CPU_ISSET(j, &cpuset))
196 printf(" CPU %d\en", j);
202 .BR sched_setaffinity (2),
204 .BR pthread_attr_setaffinity_np (3),
205 .BR pthread_self (3),
206 .BR sched_getcpu (3),