how to know the progress of each thread ?

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
an example would be helpful a lot
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
#include <future>
#include <functional>
#include <ctime>

struct progress_t
{
    std::atomic<std::thread::id> id {} ;
    std::atomic<unsigned int> how_much {} ;
    std::atomic<std::time_t> when {} ;
};

void thread_fun( progress_t& progress )
{
    progress.id = std::this_thread::get_id() ;
    progress.how_much = 0 ;

    for( int i = 0 ; i < 4 ; ++i )
    {
        // simulate progress by sleeping for one second
        std::this_thread::sleep_for( std::chrono::seconds(1) ) ;
        progress.how_much += 25 ;
        progress.when = std::time(nullptr) ;
    }
}

int main()
{
    constexpr std::size_t NTHREADS = 5 ;
    progress_t progress[NTHREADS] ;
    std::vector< std::future<void> > futures ;

    for( std::size_t i = 0 ; i < NTHREADS ; ++i )
        futures.push_back( std::async( std::launch::async, thread_fun, std::ref( progress[i] ) ) ) ;

    unsigned int total_progress = 0 ;
    while( total_progress < 100 * NTHREADS )
    {
        std::this_thread::sleep_for( std::chrono::milliseconds(1100) ) ;

        total_progress = 0 ;
        for( const auto& p : progress )
        {
            total_progress += p.how_much ;
            std::cout << "thread #" << p.id << ' ' << p.how_much << "% at " ;
            std::time_t t = p.when ;
            std::cout << std::ctime( std::addressof(t) ) << std::flush ;
        }

        std::cout << "\n\n" << std::flush ;
    }

    std::cout << "all threads reported 100% progress\n" ;
}

http://coliru.stacked-crooked.com/a/b22e718770fedf4d
@JLBorges thanks a lot
Topic archived. No new replies allowed.