mercredi 24 juin 2015

C++11 Multithreading

Machine specs : Intel i7 QuadCore, Ubuntu 15.04 64 bit.

I have a code in C++ 11 that spawns 5 threads using the new thread header file (<thread>). When I am running the program in Linux, I see from the system monitor that out of 8 possible thread only 1 is being used by my program. And as such it appears that I do not get any speed-up from single thread version of my program. Following is my way of spawning threads. Each thread does some geometric calculations.

    vector<thread> thread_pool(numberOfThreads);

    for(unsigned thread_id = 0; thread_id < numberOfThreads; thread_id++)
    {
        unsigned bound_l = N-2-thread_id*floor((N-3)/numberOfThreads);
        unsigned bound_r = N-1-(thread_id+1)*floor((N-3)/numberOfThreads);

        if(thread_id == numberOfThreads-1)
            bound_r = 2;

        thread_pool[thread_id] = thread(thread_job,bound_l,bound_r,N,thread_id);
    }

    for(unsigned thread_id = 0; thread_id < numberOfThreads; thread_id++)
        thread_pool[thread_id].join();

Is it possible to spawn the threads in different cores so that my work is done faster?

Aucun commentaire:

Enregistrer un commentaire