I have a program doing 'assignments' that are scheduled by boost::threadpool. Each of those assignments take a lot of time (1 min - 5h per assignment). I wan't to monitor progress for each assignment, to understand how long will the process take.
There are a lot more assignments then there are threads, so only NUM_THREADS
of progress outputs should be visible.
I have created a simple piece of code that does this, but there 2 problems:
- I don't think it's elegant.
- Don't know if incrementing
prog
like this is safe.
What would be the better ways of doing this?
#include <thread>
#include <iostream>
#include <atomic>
#include <vector>
volatile int prog[4]={0,0,0,0};
std::atomic<bool> prog_lock[4];
bool end(){
bool ret=true;
for(int i=0;i<4;i++)
if(prog[i]!=100){
ret=false;
break;
}
return ret;
}
void Func(){
int prog_ind=0;
for(int i=0;i<4;i++){
if(!prog_lock[i].exchange(true)){
prog_ind=i;
break;
}
}
for(int i=0;i<100;i++){
prog[prog_ind]++;
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
prog_lock[prog_ind].store(false);
}
int main(){
for(int i=0;i<4;i++)
prog_lock[i].store(false);
std::vector<std::thread> thr;
for(int i=0;i<4;i++){
thr.push_back(std::thread(Func));
}
while(!end()){
std::cout << std::string(50,' ') << "\r";
for(int i=0;i<4;i++){
std::cout << "Progress: " << prog[i] << "/100\t";
}
std::cout << "\r" << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
for(int i=0;i<4;i++)
thr[i].join();
}