1

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. 
                unsigned int step = vec.size()/SIZE;
                std::thread thread1(zeroOut,vec,step,step*2);
                std::thread thread2(zeroOut,vec,step*2,step*3);
                std::thread thread3(zeroOut,vec,step*3,step*4);
                zeroOut(vec, 0 , step);
                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.

nnrales
  • 1,481
  • 1
  • 17
  • 26
  • 4
    Have you *tried* using a vector for the threads? How did that work? How didn't it work? What problems did you encounter? – Some programmer dude Nov 26 '15 at 14:52
  • `I am thinking of using a std::vector` That's a good idea. But I can't see what specific problem do you have now. Creating the threads, splitting the work...? – deviantfan Nov 26 '15 at 14:53
  • @JoachimPileborg I do not know actually. I was thinking I could use a for loop which loops till _SIZE_ and put them in a vector ? I was wondering if it would work – nnrales Nov 26 '15 at 14:54
  • Oh, and C++ isn't Java, you don't have to use `new` to create instances. In fact, try to use as few pointers as possible. In this case I see no use for any pointers at all. – Some programmer dude Nov 26 '15 at 14:54
  • 3
    And on an unrelated note, don't use symbol names starting with an underscore followed by an upper-case letter (like you `_SIZE_` macro), [such names are reserved for the implementation](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) (compiler and standard library). – Some programmer dude Nov 26 '15 at 14:56
  • @JoachimPileborg Okay. I have gotten rid of the pointers so it will be easy to understand – nnrales Nov 26 '15 at 15:00
  • @JoachimPileborg I will try using a std::vector. Since this is new ground for me , I though there might be a better way – nnrales Nov 26 '15 at 15:01
  • @Jarod42 cleaned by the pointers. – nnrales Nov 26 '15 at 15:01

1 Answers1

2

Is this possible ?

Yes.

I am thinking of using a std::vector

Good idea. That's exactly what I recommend you to use. The number of threads in a vector can vary during runtime.

but I am new to multi threaded programming and don't know how do it.

You only use the vector of threads in the original main thread that creates the other threads. Since you use the vector in a single thread, it's use doesn't differ in any way from using a vector in a single-threaded program.

eerorika
  • 232,697
  • 12
  • 197
  • 326