[英]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++
.我已经使用
MinGW
和g++
在 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.