[英]Multithreaded C++ program using 30% CPU in Windows (compiled with MinGW), but 100% in Linux
我編寫了一個 C++ 程序,用於使用多個處理器解決一個困難的優化問題。 它的基本結構可以在下面的片段中看到。 通過使用g_thread_new
線程,使用 glib 以一種簡單的方式進行並行化。
該程序最初是在 Linux 中開發的,其中 htop 顯示它使用了所有內核的 100%。 但在 Windows 中,CPU 使用率在具有 4 個處理器 + 4 個虛擬處理器的四核計算機中達到 30-40% 左右的峰值。 我已經使用MinGW
和g++
在 Windows 中編譯了它。
為什么在 Windows 下性能如此下降? 這是由於我使用 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);
}
}
嘗試檢查值:
int processor = thread::hardware_concurrency();
該值可以不是處理器/內核數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.