jeudi 26 novembre 2015

Create variable number of std::threads

I have a threaded program that I have to run on multiple computers. Each of them have a different number of supported threads. In the computer that I developed the program there are 4 threads , and so I hard coded 4 threads to be created. I want to make this vary according to the situation . I want to use std::thread::hardware_concurrency to get the number of threads , and divide the work into the number of threads available . Is this possible ?

The hard coded thread creation is :

   //const unsigned int SIZE = std::thread::hardware_concurrency ;
    const unsigned int SIZE = 4 ;

        void zeroOut(std::vector<obj> vec , int start , int end)
       {
      // Does an operation on vec from start index to end index
       }
        int main() {
            std::vector<obj_thread>  vec ;
            // Do some work on vec. Fill it with values. 

                std::thread thread1(zeroOut,vec,SIZE/4,SIZE/2);
                std::thread thread2(zeroOut,vec,_SIZE_/2,SIZE*3/4);
                std::thread thread3(zeroOut,vec,SIZE*3/4,SIZE);
                zeroOut(vec, 0 , SIZE/4);
                thread1.join();
                thread2.join();
                thread3.join();
            return 0 ; 
            }

I am thinking of using a std::vector , but I am new to multi threaded programming and don't know how do it.

Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire