You simply call SetThreadAffinityMask
passing the handle to the thread you wish to constrain, and the processor mask for the target processor. The thread's handle can be obtained using the Handle
property.
Of course, you have to understand how to get the two threads onto the same physical core. On a hyperthreaded machine, the first N/2 logical processors are the physical cores, and the second N/2 logical processors are their hyperthreaded counterparts. So if you have a quad core, that is 8 logical processors, you want to put your threads on logical processors 0 and 4, or 1 and 5, or 2 and 6, or 3 and 7.
As general advice, you should avoid setting hard affinity masks. Scheduling threads is hard and the system generally does it better than you because it can see all the threads. You can only see your threads. You may consider SetThreadIdealProcessor
as an alternative.