为什么主线程比pthread-win32中的工作线程慢? [英] Why main thread is slower than worker thread in pthread-win32?

查看:151
本文介绍了为什么主线程比pthread-win32中的工作线程慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void* worker(void*)
{
    int clk = clock();
    float val = 0;
    for(int i = 0; i != 100000000; ++i)
    {
        val += sin(i);
    }
    printf("val: %f\n", val);
    printf("worker: %d ms\n", clock() - clk);
    return 0;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, worker, NULL);
    int clk = clock();
    float val = 0;
    for(int i = 0; i != 100000000; ++i)
    {
        val += sin(i);
    }
    printf("val: %f\n", val);
    printf("main: %d ms\n", clock() - clk);
    pthread_join(tid, 0);
    return 0;
}

主线程和工作线程应该运行速度相同,是:

Main thread and the worker thread are supposed to run equally fast, but the result is:

   val: 0.782206
   worker: 5017 ms
   val: 0.782206
   main: 8252 ms

主线程慢得多,我不知道为什么....

Main thread is much slower, I don't know why....

问题解决。这是编译器的问题,GCC(MinGW)在Windows上表现奇怪。
我在Visual Studio 2012中编译代码,没有速度差别。

Problem solved. It's the compiler's problem, GCC(MinGW) behaves weirdly on Windows. I compliled the code in Visual Studio 2012, there's no speed difference.

推荐答案

 Main thread and the worker thread are supposed to run equally fast, but the result is:

从来没有见过提供这种保证的实时操作系统外的线程系统。有了Windows线程和所有其他线程系统(我也使用posix线程,无论是MacOS X上的轻量级线程,还是C#线程中的线程),我的理解是,没有性能保证或如何快一个线程将与另一个线程相关。

I have never seen a threading system outside a realtime OS which provided such guarantees. With windows threads and all other threading systems(I have also use posix threads, and whatever the lightweight threading on MacOS X is, and threads in C# threads) in Desktop systems it is my understanding that there are no performance guarantees in terms or how fast one thread will be in relation to another.

一个可能的解释(猜测)可能是,因为你使用一个现代的四核,它可能会提高主时钟频率核心。当大多数单线程工作负载时,现代i5 / i7 / AMD-FX系统将一个内核上的时钟速率提高到预定级别,库存冷却可以消耗热量。在更多并行工作负载上,所有内核在时钟速度上获得更小的突变,再次基于散热进行预分级,并且当空闲时,所有内核被节流以最小化功耗。可能的是,背景工作的量大多在单个核上执行,并且第二线程在第二核上花费的时间量不足以证明切换到所有核速度被提升的模式。

A possible explanation (speculation) could be that since you are using a modern quadcore it could be raising the clock rate on the main core. When there are mostly single threaded workloads modern i5/i7/AMD-FX systems raise the clock rate on one core to a pre-rated level that stock cooling can dissipate the heat for. On more parallel workloads all the cores get a smaller bump in clock speed, again pre-rated based on heat dissipation and when idle all of the cores are throttled down to minimize power usage. It is possible that the amount of background work is mostly performed on a single core and the amount of time the second thread spends on the second core is not enough to justify switching to the mode where all the cores speed is boosted.

我会再试用4个线程和10倍的工作量。如果你有一个工具来监视CPU负载和时钟速度,我会检查。使用这些信息,您可以推断我是对还是错。

I would try again with 4 threads and 10x the workload. If you have a tool that monitors CPU load and clock-speeds I would check that. Using that information you can infer if I am right or wrong.

另一种选择可能是剖析,看看工作的哪部分需要时间。这可能是操作系统调用花费的时间比您的工作量更多。

Another option might be profiling and seeing if what part of the work is taking time. It could be that the OS calls are taking more time than your workload.

您还可以在另一台机器上测试您的软件,具有不同的性能特征,如稳定时钟速度或单核。这将提供更多信息。

You could also test your software on another machine with different performance characteristics such as steady clock-speed or single core. This would provide more information.

这篇关于为什么主线程比pthread-win32中的工作线程慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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