The second parameter SetThreadAffinityMask() - a bit-vector. Each bit corresponds to a logical processor: the core of the central processor or hyper-thread. If the bit in the second parameter is set to 1, the thread can start on the corresponding core.
For a core 12, your mask (1<<core)-1 contains 0..11 bits, so each thread is allowed to run on any of the 12 cores. Presumably, you wanted each thread to run in a dedicated kernel. To do this, you need each thread to have a unique number between 0 and 11 and set only the corresponding affinity mask bit. Hint: you can use InterlockedIncrement() to get a unique number. Alternatively, if your threads are all running in a loop, a unique number is already known (this is a loop counter), and you can use it, for example. jump to each thread as an argument or establish affinity for new threads in the same cycle.
And please pay attention to the caution in David Heffernan’s answer: if you don’t know how to use closeness to goodness, you better not play with closeness. In addition to the reasons David has already mentioned, I will add portability of applications on computers with different numbers of sockets, cores, and hyperthreads.
source share