Solved

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

Posted on 2008-10-28
6
621 Views
Last Modified: 2013-12-13
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!
0
Comment
Question by:rjsurati
6 Comments
 
LVL 9

Accepted Solution

by:
jhshukla earned 250 total points
ID: 22828088
http://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx
see NumberOfCores.

Here is an example to get OS info. substitute with "Win32_Processor" at appropriate place(s).
http://msdn.microsoft.com/en-us/library/aa390423(VS.85).aspx
0
 
LVL 86

Expert Comment

by:jkr
ID: 22828204
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.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 22830728
you may use multiple threads even if there is only a single processor.

The total time for all threads will be much less than processing them sequentially, especially if there is file i/o involved where the cpu must not be busy. However, you need to watch the total cpu your process takes and should provide a means to use less threads in case other processes have problems.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now