0
votes

I have a main processes that will create 4 threads. If i simply run all 4 threads would the kernel utilize all 4 cores or will the program be multithreaded on a single core? if not then how would synchronization be handled on a multicore. I have a 4core intel cpu and my program is in c++

Im running this on a linux in a Virtual machine.

1
Well, you could always just, you know... try it. The OS is pretty good at thread scheduling, I wouldn't worry too much. - Ed S.
For the first part: did you try and check? - Mat
This depends on the OS. Windows? Linux? BSD? Also on the compiler. ICC? MSVC++? GCC? TCC? - Billy ONeal
Your threads will be scheduled by the OS and run on whatever core is available. Some environments (Windows for instance) allows you to affinitize your threads to a particular core. Synchronization is (generally) handled by your kernel though your application does have to make use of the appropriate synchronization primitives. - Chad
In Linux, use grep MHz /proc/cpuinfo and see if all cores are running at full speed (in case you're running Linux and have CPU frequency scaling activated). For example, this usually gives 1197 for all my cores, if I start make -j4, 4 cores will be at max, the others still idle. - hochl

1 Answers

3
votes

You don't really know.

For one thing, the C++03 Standard doesn't know anything about threads, cores or any of that kind of stuff. So this is all platform-dependant anyway.

But even from a platform point-of-view, you often still don't really know. The operating system schedules threads and jobs. The OS might -- or might not -- give you the means to specify a "processor affinity" for a particular thread, but this typically takes some hoop-jumping-through to utilize.

One of the things you also should keep in mind is that if your goal is to keep each core 100% utilized, you'll often need more than n threads (where n is number of cores). Threads spend a lot of time sleeping, waiting on disk, and generally not doing anything on the core. The exact number of threads you'll need depends on your actual application and platform, but experimentation can help guide you towards fine tuning this.