Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been looking all over for an answer to this question. I have looked through many forum posts that have (at best) implied what the answer could be.

My question is, how do I delegate execution to a different core than the one running the main thread. I understand that with multithreading on a single core each thread executes one after the other in quick succession, but this not what I'm looking for.

What was implied in some of the forum posts that I read is that creating a new thread in the winapi (with something like _beginthread()) will automatically execute the thread on another core if one is available. I don't know for sure and if this isn't the case then what code I have been using has been wasting its available resources.
Posted

I guess what people were trying to get through to you is you should not care. Multiple threads are used to simplify the design of software that needs to do more than one thing simultaneously. A secondary benefit is improved performance if you happen to be running on a system with more than one (available) processor.

As you seem to have heard it is (rightly) up to the OS to decide which processor to run a thread on in order to maximise system performance. In fact in the OS may move threads between cores at its own whim.

But to answer your question: if you have a program with 2 (active) threads running on a system with 2 processors (cores), and there are no other active threads in the system then they will definitely be executed on different processors.
 
Share this answer
 
Comments
Rajesh R Subramanian 8-Dec-10 2:33am    
Ah, I can give a 5 for a more appropriate answer. :)
Dalek Dave 8-Dec-10 3:40am    
Good Answer.
I'm in agreement with Andrew.

You will never need to worry about thread execution at this level of detail. The OS is good enough to ensure that no resources (like a core) will be wasted sitting idle, while a thread is waiting for CPU cycles.
 
Share this answer
 
Comments
Dalek Dave 8-Dec-10 3:40am    
Sage Advice.
Threads are created using CreateThread[^], this will give you a handle to each to each thread via the return value.

By default, the operating chooses which processor to run a thread on and tries to optimise performance. However, if you want to choose for yourself, you can use the SetThreadAffinityMask[^] function to set processor affinity.
 
Share this answer
 
Comments
Rajesh R Subramanian 8-Dec-10 2:31am    
My vote of 1.
You must NEVER use CreateThread. Long story, but I've given some explanation in this thread on a different site: http://www.daniweb.com/forums/thread249210.html
[no name] 8-Dec-10 4:46am    
After reading that link, I must admit I still don't understand the problem with CreateThread.
You say using CreateThread would be a blunder since it "will be available for execution, even for a program which uses the single threaded version of the CRT". What do you mean with CRT?
Rajesh R Subramanian 8-Dec-10 6:52am    
CRT = C Run Time.
I'll try to explain it here - If you use CreateThread(), which is an API call, and is a part of Windows, it will ALWAYS create a thread, even if you're linking to the version of CRT, which is not designed to work with multiple threads running simultaneously (no thread safety). However, if you use something safer like _beginthreadex() instead of CreateThread(), the compiler will issue an error if you're linking to the single threaded version of the CRT.

Assume that there's a global variable that the CRT uses (for example - errno, which stores the last occurred error number). In a multi-threaded environment, one thread could set this errno to one thing, and another would change it at the same time, because there's no thread safety.

Read this: http://msdn.microsoft.com/en-us/library/48xz4yz9(VS.80).aspx
Also, see the "remarks" section of CWinThread: http://msdn.microsoft.com/en-us/library/48xz4yz9(VS.80).aspx
[no name] 8-Dec-10 6:57am    
That makes sense, thanks :)
Rajesh R Subramanian 8-Dec-10 7:03am    
Glad to be of help. :)

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