简体   繁体   中英

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. 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 .

The program was originally developed in Linux, where htop shows that it uses 100% of all cores. But in Windows the CPU usage peaks at around 30-40% in a quad-core computer with 4 processors + 4 virtual processors. I have compiled it in Windows using MinGW and g++ .

Why is the performance so degraded under Windows? Is this caused by the fact that I compiled the program using 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();

the value can be other than processors/cores amount.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM