Defined in carb/thread/Util.h

inline bool carb::thread::setAffinity(NativeHandleType h, const CpuMaskVector &masks)

Sets the CPU Affinity for the thread.

On Windows each entry in the CpuMaskVector represents a Processor Group. Each thread can only belong to a single Processor Group, so this function will only set the CPU Affinity to the first non-zero entry in the provided CpuMaskVector. That is to say, if both masks[0] and masks[1] both have bits sets, only the CPUs in masks[0] will be set for the affinity.

On Linux, the CpuMaskVector is analogous to a cpu_set_t. There are no restrictions on the number of CPUs that the affinity mask can contain.


On Mac OS, the CPU affinity works differently than on other systems. The mask is treated as a unique ID for groups of threads that should run on cores that share L2 cache, rather than specific CPUs. For single CPU masks, this will function somewhat similarly to other systems, but threads won’t be pinned to a specific core.

  • h – The thread to set CPU Affinity for.

  • masks – Affinity masks to set.


True if the function succeeded, false otherwise. If masks is empty, or has no bits set, false will be returned. If the underlying function for setting affinity failed, then errno or last-error will be set.