There is a class. How to make parallel creation of class instances? Those. The constructor of this class is quite capacious logic. I want to create separate threads for each instance, then run them together independently of each other, and wait for the execution of all threads.

  • An example using C ++ 11 is suitable? - cybrex

1 answer 1

You can do this:

concurrency::task<A*> task1([=](){ return new A(); }); concurrency::task<A*> task2([=](){ return new A(); }); (task1 && task2).wait(); A* obj1 = task1.get(); A* obj2 = task2.get(); 

or so:

 std::future<A*> f1 = std::async([=](){ return new A(); }); std::future<A*> f2 = std::async([=](){ return new A(); }); f1.wait(); f2.wait(); A* obj1 = f1.get(); A* obj2 = f2.get(); 
  • I don’t think that C ++ 11 code will help me in Qt :( - Oleg C
  • @OlegS, The code is also compiled in Qt (checked on Qt 5.3.2 with the compiler from MSVC2013). The resulting code will be cross-platform, if that's what's bothering you. - cybrex
  • It also seems to me that you have been answered exactly to the question asked. For kosher, all the same you have Qt5, you can use QtConcurrent. Another thing is that architecturally heavy designers are strange. - erapid