# How to set thread affinity? (Which processor core it runs on) C# Resolved!



## JTD92

Run Task Manager and right click the process that your running and click "set affinity" then check what core you want it on like. (Core 0, Core 1)


----------



## Licht

Quote:


Originally Posted by *JTD92* 
Run Task Manager and right click the process that your running and click "set affinity" then check what core you want it on like. (Core 0, Core 1)

Thread affinity, not the entire process' affinity. The program is multi-threaded.


----------



## Polska

I have never bothered playing with threading, but maybe you can have a look here (maybe you have).

System.Threading.Thread.BeginThreadAffinity

Sorry I couldn't be of more help, maybe its a step in the right direction.


----------



## Licht

Quote:


Originally Posted by *Polska* 
I have never bothered playing with threading, but maybe you can have a look here (maybe you have).

System.Threading.Thread.BeginThreadAffinity

Sorry I couldn't be of more help, maybe its a step in the right direction.

Thats the function however it isn't telling me how to use it.


----------



## NP2H

Quote:


Originally Posted by *Licht* 
Thats the function however it isn't telling me how to use it.

I think the code you're looking at is going to prevent the core from switching to another thread until its critical region (the code between begin/end) has been executed.

Based on your statement of the load running better if it's one thread or two, that should be written into the code itself, should it not? Forgive me, I am not terribly educated into the programming language yet, 2nd year student









I.e. the program could be multi-threaded but the 'load' executed by the program should be written into a thread of its own.


----------



## Licht

Quote:


Originally Posted by *NP2H* 
I think the code you're looking at is going to prevent the core from switching to another thread until its critical region (the code between begin/end) has been executed.

Based on your statement of the load running better if it's one thread or two, that should be written into the code itself, should it not? Forgive me, I am not terribly educated into the programming language yet, 2nd year student









I.e. the program could be multi-threaded but the 'load' executed by the program should be written into a thread of its own.

That made no sense at all...


----------



## NP2H

Quote:


Originally Posted by *Licht* 
That made no sense at all...

Well, like I said, I'm not a very experienced programmer.

I've used fork() for linux based threads, Thread in Java, and Pthread in C.

I'll try again.

Put the code that executes the thread that you'd like to remain on 1 core within the begin/end commands of the affinity function outlined above and see if that fixes your problem.

If i'm still not making sense, or I clearly am not educated enough to help, then I apologize in advance; just trying to shed some new light on it.


----------



## Licht

Quote:


Originally Posted by *NP2H* 
Well, like I said, I'm not a very experienced programmer.

I've used fork() for linux based threads, Thread in Java, and Pthread in C.

I'll try again.

Put the code that executes the thread that you'd like to remain on 1 core within the begin/end commands of the affinity function outlined above and see if that fixes your problem.

If i'm still not making sense, or I clearly am not educated enough to help, then I apologize in advance; just trying to shed some new light on it.

Just gimme a simple coding example of how to set the affinity is all i want.


----------



## arjo_reich

(it's been a while since I've done anything out of the routine/mundane so my threading is a bit v1.1 biased, there may be some things in v2.0 or v3.5 that contradict me...)

That being said, I'm curious to know what you're attempting to do with the thread. The threading in the managed environmnet is geared towards background/worker threads that are sent off to accomplish some mundane task (printing a document, frx) while freeing up the main/ui thread to do it's thing and process all the window messages like a good pup, lol.

And IIRC, BeginThreadAffinity/EndThreadAffinity has nothing to do with which processor is used, it's a "safe-guard" to mark code within it's BEGIN/END block as being unsafe for thread-managers to redistribute between it's thread-pool. The online help gives SQL Server 2005's thread/fiber manager as an example...


----------



## Masterchief3k

Try using AMD Dual Core Optimizer.


----------



## Licht

Quote:


Originally Posted by *Masterchief3k* 
Try using AMD Dual Core Optimizer.

What is it with people and posting irrelevant things in this thread?... (No offense but if you don't understand what i am talking about just ask.)

No AMD dual core optimizer is already installed and won't help. This error also occurs on Intel rigs. I need to get down into the nitty gritty and control the code on a seriously low level i think. Besides, the dual core optimizer spreads the work load across cores, this is the opposite of what i want, i want total control without interference.


----------



## arjo_reich

Quote:


Originally Posted by *Licht* 
What is it with people and posting irrelevant things in this thread?... (No offense but if you don't understand what i am talking about just ask.)

i want total control without interference.

That won't be likely in a managed environment like the .NET Framework... in fact you could go so far as to say that they're completely mutually exclusive terms.


----------



## arjo_reich

UPDATE:

I was doing a little digging - bored as I was in the process of building a new vm-workstation for vs.net 2008 development in a Vista x64 platform - and I found something that you might be able to look into...

Parallel Performance: Optimize Managed Code For Multi-Core Machines

Which uses the Parallel Extensions - which is only available in the .NET 3.5 framework which is in CTP (community technology preview) before it's finally released...
http://www.microsoft.com/downloads/d...displaylang=en


----------



## Masterchief3k

Woah licht, i didn't know, so chill man.


----------



## Licht

Quote:


Originally Posted by *arjo_reich* 
UPDATE:

I was doing a little digging - bored as I was in the process of building a new vm-workstation for vs.net 2008 development in a Vista x64 platform - and I found something that you might be able to look into...

Parallel Performance: Optimize Managed Code For Multi-Core Machines

Which uses the Parallel Extensions - which is only available in the .NET 3.5 framework which is in CTP (community technology preview) before it's finally released...
http://www.microsoft.com/downloads/d...displaylang=en

I have 3.5 running on this rig. Hopefully the final release will be soon.

Quote:

Woah licht, i didn't know, so chill man.
My attitude never changed, although i'm told i seam emotionless if that is what your revering to.


----------



## Licht

http://msdn2.microsoft.com/en-us/lib...47(VS.85).aspx

Holy shazbot...

http://forums.microsoft.com/MSDN/Sho...iteID=1&mode=1

Looks like i'm using Kernel32.DLL one way or another. Anyone else have info on it?

*Resolved!!*


----------

