Jul 31, 2018 at 11:03am Jul 31, 2018 at 11:03am UTC
Hey,
In fact, I'm a beginner developer in C++, and while dealing with threads to process files, dig data structures etc, a question comes to my mind.
how do I know the progress of each thread?
So I came up with this idea create for each thread its own variable which will be updated something like "progressTh1++" inside a loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// inside main
double progressTh1 = 0;
double progressTh2 = 0;
double progressTh3 = 0;
vector<double > progress;
progress.push_back(progressTh1);
progress.push_back(progressTh2);
progress.push_back(progressTh3);
std::async(Processing, std::ref(progressTh1));
std::async(Processing, std::ref(progressTh2));
std::async(Processing, std::ref(progressTh3));
//function
void Processing( double & progressTh){
// do something
for (int i = 0; i < 10; i++
{
progressTh++;
}
}
and write another function which just read these progress variable in other thread
1 2 3 4 5 6 7 8 9 10 11
// inside main
std::async(showProgress, std::ref(progress));
// function
void showProgress(vector<double > &progress){
cout << "showing ! " << endl;
for (auto i : progress)
{
cout << i<< endl;
}
}
I tried this but it's not working
Last edited on Jul 31, 2018 at 11:05am Jul 31, 2018 at 11:05am UTC
Jul 31, 2018 at 11:07am Jul 31, 2018 at 11:07am UTC
an example would be helpful a lot