'c++ : updating progress bar from std::async

It is an all time classics problem, but to be honest I haven't found a simple example of how to update a progress bar by an async function call.

lets say I have(not the accurate code, but the main flow is like this):

int main (){

    ProgressBar progressBar = new ProgressBar();


    for (int i = 0; i < cores; i++) {
      std::shared_future<MyObj*> f = std::async(std::launch::async, myFunc, 
       myFunctionArgument);
      futures.push_back(f);
    }


    std::vector<MyObj*> resCollects;
    for (std::shared_future<MyObj*> f : futures)
    {
      MyObj* res = f.get();
      resCollects.push_back(res);
    }
return;
}

The computationally heavy task that has to be carried out async the job per core is here:

    MyObj* myFunc (MyFunctionArgument myFunctionArgument){
     for(int i=0; i < myFunctionArgument.size(); i++){
       doLongCalculations(myFunctionArgument.data);
       //HOW DO I UPDATE THE PROGRESS BAR THAT A SMALL FRAGMENT OF THE TASK HAS BEEN 
       //COMPLETED? updateProgressBar(int i);
     }
    }

The class ProgressBar is in a simple form:

    class ProgressBar{
      private: 
                int totalTasks;
                int currentProgress;
     public:
       void updateProgressBar(int i){
         cout<<" The current progress is: " << i << "out of "<< 
     totalTasks;
        }
    
    }

Supposing I have already implemented a simple class of progress bar which takes an integer and updates the percentage of the total task(of which I know the total size) that has been completed, what's the basic design of updating that progress bar? Where should i call "updateProgressBar(int i) from? I need a very simple example to understand the design of the solution how to emit and receive progress.

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source