Link to home
Start Free TrialLog in
Avatar of rjsurati
rjsurati

asked on

How does one spawn one thread for each core in C++ on Windows?

I'm writing an image processing algorithm to process a bunch of completely separate images. In an ideal world, I would like to query the operating system for how many cores it has available, and then run a thread on each core.

Is there a way in C++ to query for how many cores are on the machine on which I am running?
Can I spawn a thread for a particular core?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of jhshukla
jhshukla
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
Using the Windows API, you can always call 'SetThreadIdealProcessor()' (http://msdn.microsoft.com/en-us/library/ms686253.aspx) after you created the thread. You might want to create it in a 'suspended' state, set the processor and then resume it. For the amount of cores, there is 'GetSystemInfo()' (http://msdn.microsoft.com/en-us/library/ms724381.aspx). The structure this API fills in has a member called 'dwNumberOfProcessors' which will tell you the number of cores available.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial