Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In C# can I specify a preferred core number for a thread to be executed?
Example:- if there are two threads and the PC has two cores. Do I have the option of specifying, Thread one execute on core one and Thread two execute on core two?
Posted

You can use System.Threading.Thread.BeginThreadAffinity/EndThreadAffinity, for compact Framework — System.Threading.Thread.SetProcessorAffinity.

Use with care, it can potentially do more harm than help. I would only use to serve some specific hardware which require polling, for example.

—SA
 
Share this answer
 
Comments
Rick Shaub 23-Mar-11 20:35pm    
I don't think this is a Compact Framework question.
Sergey Alexandrovich Kryukov 23-Mar-11 21:02pm    
This is mentioned only for completeness, only for the last API; what's wrong about it?
Pay attention for my punctuation... Nothing wrong, I guess??
--SA
Rick Shaub 24-Mar-11 10:35am    
BeginThreadAffinity/EndThreadAffinity don't do the same thing as the OP asked.
Sergey Alexandrovich Kryukov 24-Mar-11 12:04pm    
I wonder why.
--SA
Panchaz 23-Mar-11 23:03pm    
This is not a .NET Compact Framework problem. :(
You can using p/invoke.
First you want to create a thread using the CreateThread[^]function.
Then, pass the return value from CreateThread to SetThreadIdealProcessor[^]
 
Share this answer
 
Comments
Panchaz 23-Mar-11 11:12am    
can i do this on 64bit platform?
Rick Shaub 23-Mar-11 11:23am    
It should work especially if you are compiling on a 64 bit system. It looks like stack alignment can be troublesome in some cases see http://stackoverflow.com/questions/3046439/createthread-fails-on-64-bit-windows-works-on-32-bit-windows-why and http://msdn.microsoft.com/en-us/library/aa290049%28VS.71%29.aspx
Sergey Alexandrovich Kryukov 23-Mar-11 20:26pm    
I don't think it needs P/Invoke. This is a matter of thread affinity; please see my Answer.
(I did not vote.)
--SA
Yes, but you're wasting your time. Windows/.Net assigns core affinity for threads and processes automagically.

EDIT ========

1-voting this isn't going to change the truth of what I say.
 
Share this answer
 
v2
Comments
Panchaz 23-Mar-11 10:59am    
i know but this is a requirement
#realJSOP 23-Mar-11 11:27am    
What if the core number you want to use isn't implemented on the system your app is on? The "requirement" is artificial and arbitrary given .Net's ability to handle this for you.
Dave Kreskowiak 24-Mar-11 0:03am    
It's a stupid requirement. What if the core is tied up with other threads, but there's an available core that isn't doing anything? Your code has to wait. You're actually sacrificing performance, not gaining it.
Rick Shaub 23-Mar-11 12:55pm    
FYI- I agree that in most cases it's a waste of time. I just answered his question. I also didn't 1 vote you.
#realJSOP 23-Mar-11 21:14pm    
I know - that 1-vote comment was for everyone. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900