Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-28
6
Medium Priority
?
634 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 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Or at least that’s the word according to a new blog from Tech Target on AWS’s new Managed Services (MS) offering. According to the blog, AWS is launching their AWS MS program to expedite the adoption of cloud by Fortune 1000 and Global 2000 companie…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

926 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