views:

370

answers:

2

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.

A: 

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

Axarydax
But How to run threads on typical core(s) ?
DotNetBeginner
@Brian Rasmussen - and what? I stand behind my words, that there is no way from .NET to force particular thread to execute on particular processor. See this thread for details: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/3279302a-6786-44a7-b69d-c302699b6fcb
Axarydax
@Axarydax: Sorry, I misunderstood you then.
Brian Rasmussen
+2  A: 

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!

Mef
I have tried using SetThreadAffinityMask(), plese have a look at my question @ http://stackoverflow.com/questions/2498501/using-setthreadaffinitymask-function-imported-from-kernel32-dll-in-c-code
DotNetBeginner