simonc
simonc

Reputation: 42215

Instruct win32 threads to run on a single processor core

I have a test program which would be much simpler if it could rely on threads being scheduled in strict priority order on Windows. I'm seeing a low priority thread running alongside higher priority threads and wonder if this is happening because the different threads are being scheduled on different processor cores.

Is there a way to force all Win32 threads in a process to use a single processor core? SetThreadAffinityMask looks like it might be interesting but its docs aren't entirely clear and I'm not sure how to use it.

Upvotes: 4

Views: 2114

Answers (2)

Louis Ricci
Louis Ricci

Reputation: 21116

SetThreadAffinityMask function: Sets a processor affinity mask for the specified thread.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686247%28v=vs.85%29.aspx

SetThreadAffinityMask(GetCurrentThread(), (1 << CoreNumber));

Sets the current thread's affinity to 'CoreNumber' variable

Upvotes: 6

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

Even if you force all threads onto one virtual processor you will still often have low-priority threads running and high-priority threads waiting for them (priority inversion). Once a thread is scheduled by the windows-scheduler it runs until it is either preempted or sleeps (or some other sleep-inducing system call). You will have to change the design of your application so that it no-longer assumes that no low-priority thread runs while a high-priority thread would be ready to run also.

Upvotes: 0

Related Questions