I have a very similar problem to this. Unfortunately, I am not allowed to comment on it so please excuse me for opening up another topic for this. My code is running a two-stage calculation iteratively which in principle looks like this:
while(!finishing_condition_met)
{
boost::thread_group executionGrp1;
for(int w = 0; w < numThreads; w++)
{
boost::thread * curThread = new boost::thread(&Class::operation1, this, boost::ref(argument1), ...);
executionGrp1.add_thread(curThread);
}
executionGrp1.join_all();
boost::thread_group executionGrp2;
for(int w = 0; w < numThreads; w++)
{
boost::thread * curThread = new boost::thread(&Class::operation2, this, boost::ref(argument1), ...);
executionGrp2.add_thread(curThread);
}
executionGrp2.join_all();
update_finished_criterion();
}
Since numThreads is significantly smaller than what the kernel would allow (it is set to hardware concurrency which is 56 on the current machine), I was surprised see this error. Does join_all() not take care of the finished threads? The thread_pool-approach suggested in the other post seems interesting but I am not exactly sure how to adapt it such that I can rerun everything within the loop multiple times while still waiting for the first stage to finish before starting the second stage.
Any suggestions are welcome! Thanks in advance.
EDIT: This is how I can cause this error in a minimalistic fashion. AFAIK, this is the standard way to implement parallel sections. Am I missing something?
#include "boost/thread.hpp"
#include "boost/chrono.hpp"
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
int numThreads = boost::thread::hardware_concurrency();
void wait(int seconds) {
boost::this_thread::sleep_for(boost::chrono::milliseconds(seconds));
return;
}
int subthread(int i) {
wait(i/numThreads);
return 1;
}
void threads(int nT) {
boost::thread_group exeGrp;
for (int i=0;i<nT;i++) {
boost::thread * curThread = new boost::thread(&subthread, i);
exeGrp.add_thread(curThread);
}
exeGrp.join_all();
}
int main() {
for (int a=0;a<numThreads;a++) {
cout << "Starting " << numThreads << " threads [" << a << "/" << numThreads << "]" << endl;
threads(numThreads);
}
cout << "done" << endl;
}