Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Eclipse Neon start with Admin account only 6 91
tvirtualstringtree freeze when load too manny images 10 52
Which memory brick is defective ? 8 69
A short article about a problem I had getting the GPS LocationListener working.
Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.

911 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

19 Experts available now in Live!

Get 1:1 Help Now