Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between thread affinity and process affinity?

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.

like image 918
DotNetBeginner Avatar asked Mar 24 '10 11:03

DotNetBeginner


1 Answers

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!

like image 109
Leo Avatar answered Nov 06 '22 01:11

Leo