p The .Fn pthread_setaffinity_np function sets the affinity mask .Fa set for .Fa thread . At least one valid CPU must be set in the mask.
p The .Fn pthread_getaffinity_np function gets the affinity mask of .Fa thread into .Fa set . Note that .Fa set must be created and initialized using the .Xr cpuset 3 functions. .Sh IMPLEMENTATION NOTES Setting CPU .Nm requires super-user privileges. Ordinary users can be allowed to control CPU affinity of their threads via the
a security.models.extensions.user_set_cpu_affinity .Xr sysctl 7 . See .Xr secmodel_extensions 9 .
p Portable applications should not use the .Fn pthread_setaffinity_np and .Fn pthread_getaffinity_np functions. .Sh RETURN VALUES The .Fn pthread_setaffinity_np and .Fn pthread_getaffinity_np functions return 0 on success. Otherwise, an error number is returned to indicate the error. .Sh EXAMPLES An example of code fragment, which sets the affinity for the current thread to the CPU whose ID is 0: d -literal cpuset_t *cset; pthread_t pth; cpuid_t ci; int error; cset = cpuset_create(); if (cset == NULL) { err(EXIT_FAILURE, "cpuset_create"); } ci = 0; cpuset_set(ci, cset); pth = pthread_self(); error = pthread_setaffinity_np(pth, cpuset_size(cset), cset); if (error) { errc(EXIT_FAILURE, error, "pthread_setaffinity_np failed"); } cpuset_destroy(cset); .Ed .Sh COMPATIBILITY Both functions are non-standard extensions. .Sh ERRORS Both functions may fail if: l -tag -width Er t Bq Er EINVAL The specified .Fa set was invalid. t Bq Er EPERM The calling process lacks the appropriate privileges to perform the operation. t Bq Er ESRCH No thread could be found corresponding to the one specified by .Fa thread . .El .Sh NOTES There is an alternative processor sets interface, see .Xr pset 3 . However, thread affinity and processor sets are mutually exclusive, hence mixing of these interfaces is prohibited. .Sh SEE ALSO .Xr cpuset 3 , .Xr pset 3 , .Xr pthread_getschedparam 3 , .Xr pthread_setschedparam 3 , .Xr sched 3 , .Xr schedctl 8