vendredi 3 août 2018

Master thread slower than other threads

I have a multi-threaded program in C++. I am wondering why if the master thread does part of the computation, it is always (in my test) that the master thread is a couple seconds slower than the rest of the threads. (for comparison other threads run in 9 seconds, master thread run in 11 seconds).

If I don't use the master thread for part of the calculations, all the threads complete at about the same time. i.e. :

int numThreads = 4;
std::thread *threads = new std::thread[numThreads];

for (int i = 0; i < numThreads; i++){
   threads[i] = std::thread(func)
}

for (int i = 0; i < numThreads; i++){
    threads[i].join();
}

vs

int numThreads = 4;
std::thread *threads = new std::thread[numThreads - 1];

for (int i = 0; i < numThreads-1; i++){
   threads[i] = std::thread(func);
}

func();

for (int i = 0; i < numThreads-1; i++){
   threads[i].join();
}

Thank you very much!

Aucun commentaire:

Enregistrer un commentaire