12
votes

What is the difference between thread affinity and process affinity?

If I have two threads and I have a dual core machine then is it possible to run these two threads in parallel on the two cores?

If I use processor affinity mask then I can control execution of a process on the cores but when I have to run threads on a particular core how can I make these threads core specific?

A very simple example will be appreciated.

2

2 Answers

4
votes

What is difference between Thread Affinity and Process affinity ?

The process affinity is the default affinity mask for all threads belonging to that process. New threads will start with the process affinity mask if not specified otherwise. However, the affinity of a single thread can be changed without changing the process affinity (and the affinity masks of the other threads), and that's when there is a difference between process and thread affinties.

If I have two Threads and I have duel core machine then is it possible to run these two threads parallely on the two cores ?

Yes it is possible, but in most cases, you really should let the operating system decide... most likely it is smarter than you.

If I use processor affinity Mask then I can control execution of a process on the cores but when I have to run threads on a particular core how can I make these threads core specific ?

You would have to use p/invoke to call the unmanaged function SetThreadAffinityMask() and then use Thread.BeginThreadAffinity() to ensure that the managed thread stays with a specific OS thread. But you really don't want to do that!

0
votes

I am not aware that you can set thread affinity in .net, so environment decides which threads to run on which core.