双核机器上的多线程? [英] multithreading on dual core machine?

查看:118
本文介绍了双核机器上的多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双核处理器,根据解释,我只能使用2个线程,但实际上我可以同时启动超过2个线程:



以下是解释的副本:



提供的静态hardware_concurrency :thread类,返回可以
物理上同时执行的线程数,基于CPU或CPU核心的基础数量
。在常用的
双核机器上调用此函数,返回值为2。这允许一个简单的
方法来识别
应该由给定的多线程应用程序同时使用的理论最大线程数。


hardware_concurrency()方法在我的例子中返回2,但此程序同时使用4个线程:

  #include  #include< boost\thread.hpp> 

using namespace std;
using boost :: thread;
using namespace boost :: this_thread;
使用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个线程(没问题)
线程thr1(f1);
thread thr2(f2);
thread thr3(f1);
thread thr4(f2);
cin.ignore();
return 0;
}

任何人都可以解释这种行为吗?

线程通常包含三个抽象层:


  1. 线程是由应用程序启动的线程,并将N:M映射到:

  2. 内核线程,由操作系统管理, :M to:

  3. 硬件线程,这是实际可用的物理资源。

你说的应用程序启动的4个线程来自类别1(用户线程),而该函数返回的值2指的是类别3(硬件线程)。由于跨层的映射是N:M,您可以看到您可以有几个用户线程映射到较少数量的硬件线程。



这样,如果你进行密集型计算,通常启动超过2倍的硬件线程数会损害性能,因为上下文切换和资源争用。


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?

解决方案

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.

这篇关于双核机器上的多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆