I want to simulate a scenario multiple times, i.e., repCnt = 100. To speed up the process, I want to use multiple threads, which work in parallel and use a mutex when they have to log the results into a file.
They need to deduct from the total amount of repetitions by working in groups of NUM_THREADS = 4. The logging part with mutex is easy, but I can not figure out how the main loop should look like.
Here is a start:
NUM_THREADS = 4
void simulate(struct argType arg) {
}
int main() {
// ... Some code here ...
vector<thread> vecOfThreads;
for (rep = 0; rep < repCnt; rep++) {
// Here they should work in groups of 4, i.e., rep = 1, 2, 3, 4
// They need to call the simulate(struct argType) function while they are working
// Once a thread is done, it should get the next task from the loop, i.e., rep = 5
}
return 0;
}
When I searched for threadpooling in C++, all I found was with too many classes and methods. There should be a much faster way of doing what I want to achieve. Can someone help me out with the simplest and shortest C++ code?