9

I have a dual core processor and according to the explanation I'm able to use only 2 threads but actually I'm able to launch more than 2 threads at same time:

Here is a copy of the explanation:

The static hardware_concurrency() method, provided by the boost::thread class, returns the number of threads that could physically be executed at the same time based on the underlying number of CPUs or CPU cores. Calling this function on a commonly used dual-core machine, a value of 2 is returned. This allows for a simple method to identify the theoretical maximum number of threads that should be used simultaneously by a given multithreaded application.

hardware_concurrency() method returns number 2 in my case, but this program uses 4 threads at same time:

#include <iostream>
#include <boost\thread.hpp>

using namespace std;
using boost::thread;
using namespace boost::this_thread;
using boost::posix_time::seconds;

void f1()
{
    for(int i = 0; i < 10; ++i)
    {
        cout << i << endl;
        sleep(seconds(2));
    }
}

void f2()
{
    for(int i = 0; i < 10; ++i)
    {
        cout << i << endl;
        sleep(seconds(2));
    }
}

int main()
{
    // 4 threads are executed on dual core machine (no problem)
    thread thr1(f1);
    thread thr2(f2);
    thread thr3(f1);
    thread thr4(f2);
    cin.ignore();
    return 0;
}

Can anyone explain that behavior?

crashmstr
  • 28,043
  • 9
  • 61
  • 79
codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • 7
    You can make as many threads as you want (up to the limit), but 2 is (probably) the optimal number, one for each core. Also, it says "physically executed at the same time". You can make more than 2, but they will not be physically running at the same time; the OS will have to preempt running ones to let others run. Also, it says "should", not "can". – Seth Carnegie Jan 10 '12 at 20:02
  • and what are side effects by using for example 10 threaded application on dualcore machine? is that bad or not? thanks alot. – codekiddy Jan 10 '12 at 20:04
  • 1
    The side effects are (probably) limited to your application running slower (since the OS has to worry about letting every other thread have its time to run, where with 2, it lets both run full speed continuously), although that may be offset by being able to do more "at once" (not really at once, but independently). – Seth Carnegie Jan 10 '12 at 20:05
  • It creates unneeded overhead for thread management. – Stephan Dollberg Jan 10 '12 at 20:06
  • 1
    Also note that those 10 threads aren't alone by any strech. According to the task manager my computer runs 732 threads in 58 processes right now. Then again, most of those processes might be sleeping. –  Jan 10 '12 at 20:12

5 Answers5

25

The term threads usually covers three abstraction layers:

  1. User threads are threads launched by applications and are mapped N:M to:
  2. Kernel threads, which are threads managed by the operating system, mapped N:M to:
  3. Hardware threads, which are the actual physical resources available.

The 4 threads you said are launched by the application are from category 1 (user threads), while the value 2 returned by that function refers to category 3 (hardware threads). Since the mapping is N:M across the layers, you can see that you can have several user threads mapped to a smaller number of hardware threads.

Having said this, typically starting more than 2x the number of hardware threads if you are doing intensive computations will hurt performance due to context switches and resource contention.

Tudor
  • 61,523
  • 12
  • 102
  • 142
6

You can always run multiple threads, even on single core machine. Though, they can't run in parallel. (more than 2 in your case)

For example, one thread does the GUI, the other fetches some work from the server...

See this for a deeper explanation.

Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
1

You can use more threads than you have processor cores. This can have several benefits: you can hide communication (e.g., file I/O or network) with computation, or get more processor time in time-slicing systems. With two cores, only two threads will physically be executing at the same time, but having more threads can increase performance. It's something you need to tune.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
1

There's a difference between optimal and possible results; there also is a difference between threads phisically and theoretically running at the same time. In a dual-core computer, there are 2 CPUs, that are able to execute two threads phisically at the same time. But the significance of any threading system/thread library is that you are able to create logically as meany threads as you want. These threads won't really run at the same time, they'll be switched over periodically instead to make the illusion as if they were running simoultaneously.

1

The first called paralell programming and the second one is multi tasking which can be done even in a single processor machine

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70