Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multithreading on dual core machine?

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?

like image 718
codekiddy Avatar asked Jan 10 '12 20:01

codekiddy


People also ask

What is dual core multi threading?

Dual-core systems enable multitasking operating systems to execute two tasks simultaneously. The OS executes multiple applications more efficiently by splitting the different applications, or processes, between the separate CPU cores.

How many threads can run on 2 cores?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

Do you need multiple cores for multithreading?

In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. This approach differs from multiprocessing.

Can you do multithreading with one core?

Yes, you can have multiple threads on a single-core computer. The difference between single processor and multi-processor systems is that a multi-processor system can indeed do more than one thing at a time.


2 Answers

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.

like image 59
Tudor Avatar answered Oct 30 '22 20:10

Tudor


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.

like image 24
Stephan Dollberg Avatar answered Oct 30 '22 20:10

Stephan Dollberg