I just started with multithreading. The problem is that my code does not start a new thread after completion, but starts 4 threads after the last 4 is completed, so if tasks take an unequal amount of time, this is not very efficient, and I have thousands of classes to process each time.
Loop() { myClass A,B,C,D; std::thread td1(&myClass::run, &A, args[i]); std::thread td2(&myClass::run, &B, args[i+1]); std::thread td3(&myClass::run, &C, args[i+2]); std::thread td4(&myClass::run, &D, args[i+3]); td1.join(); td2.join(); td3.join(); td4.join(); }
I found this one , but it didn’t help me at all. I was thinking of using std :: prom, std :: future or std :: packaged_task to keep track of whether a thread ends, but I don't know how to create another thread in its place.
In my case, I just need X threads that run my method with different arguments and their own clean myClass object. I cannot send my calculation work, since it needs all the data that goes into the class.
source share