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!