Solved

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

Posted on 2008-10-28
6
623 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

803 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