简体   繁体   English

多线程 C++ 程序在 Windows 中使用 30% CPU(使用 MinGW 编译),但在 Linux 中使用 100%

[英]Multithreaded C++ program using 30% CPU in Windows (compiled with MinGW), but 100% in Linux

I have written a C++ program for solving a difficult optimization problem using multiple processors.我编写了一个 C++ 程序,用于使用多个处理器解决一个困难的优化问题。 Its basic structure can be seen in the snippet below.它的基本结构可以在下面的片段中看到。 The paralellization is made in a simple way using glib, by spawning threads with g_thread_new .通过使用g_thread_new线程,使用 glib 以一种简单的方式进行并行化。

The program was originally developed in Linux, where htop shows that it uses 100% of all cores.该程序最初是在 Linux 中开发的,其中 htop 显示它使用了所有内核的 100%。 But in Windows the CPU usage peaks at around 30-40% in a quad-core computer with 4 processors + 4 virtual processors.但在 Windows 中,CPU 使用率在具有 4 个处理器 + 4 个虚拟处理器的四核计算机中达到 30-40% 左右的峰值。 I have compiled it in Windows using MinGW and g++ .我已经使用MinGWg++在 Windows 中编译了它。

Why is the performance so degraded under Windows?为什么在 Windows 下性能如此下降? Is this caused by the fact that I compiled the program using MinGW?这是由于我使用 MinGW 编译程序造成的吗?

#include <gtk/gtk.h>
#include <thread>

using namespace std;

void intensive_function() {
    //... heavy computations
    return;
}

static gpointer worker(gpointer data) {
    intensive_function();
    return NULL;
}

int main(int argc, char *argv[]) {
    int processors = thread::hardware_concurrency();

    for(int i = 0; i < processors; i++) {
        GThread *thread;
        thread = g_thread_new("worker", worker, NULL);
        g_thread_unref(thread);
    }
}

Try to check value:尝试检查值:

int processors = thread::hardware_concurrency(); int processor = thread::hardware_concurrency();

the value can be other than processors/cores amount.该值可以不是处理器/内核数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用100%CPU的C ++编译代码 - C++ compiled code using 100% CPU 在Windows上编译MinGW的C ++程序是否适用于Linux上的GNU编译器? - Will a C++ program that compiles for MinGW on Windows work for GNU compiler on Linux? 使用mingw在Linux上进行C ++程序交叉编译可在msys2中工作,但不能直接在Windows中工作 - C++ program cross compile on linux using mingw works in msys2 but not directly in windows C++ Windows 编译程序与 Linux 的行为不同 - C++ Windows compiled program different behaviour to Linux 在C ++中从多线程CPU程序转向GPU - Moving from Multithreaded CPU program to GPU in C++ C ++-如何使用MinGW独立编译的程序? - C++ - How to make a program compiled with MinGW standalone? 用MinGW编译的C ++ HelloWorld程序因“非法参数”而崩溃 - C++ HelloWorld program compiled with MinGW crashes with “Illegal Argument” 如何在Eclipse中运行用MinGW编译的C ++程序? 如何“链接(?)”? - How to run a C++ program compiled with MinGW in Eclipse? How to “link(?)”? 在MinGW编译的C++程序中播放音乐文件 - Play a music file in C++ program being compiled by MinGW C++ 多线程程序在 Windows 中工作正常,在 Linux 中抛出“资源暂时不可用” - C++ multithreaded program work fine in windows, throws "resource temporarily unavailable" in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM