SetThreadAffinityMask for unix systems
I would like to prevent a thread being executed on more than one core, respectively I don't want that when a certain thread is being executed on one core, it should not be scheduled to be executed on another core.
I use the x86 instruction RDTSC to generate timestamps. When this instruction i开发者_开发技巧s called from different CPU cores, different timestamp counters might be used and since the different counters are not synchronized through the CPU cores, there might emerge inconsistent results.
On windows there is a function to force this behavior:
DWORD_PTR WINAPI SetThreadAffinityMask(
__in HANDLE hThread,
__in DWORD_PTR dwThreadAffinityMask
);
Sets a processor affinity mask for the specified thread.
MSDN link
Is there something similiar for unix systems?
With Solaris, you can use CPU binding which can be applied to single threads, single processes, set of processes or all processes in a zone. See processor_bind and pbind.
Linux 2.5.8 and higher has sched_setaffinity(). It's often used at the process level, but also works with threads:
The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to
gettid
(2) can be passed in the argumentpid
.
精彩评论