Directory: | ./ |
---|---|
File: | TESTS/TEST_MULTITHREAD_PROGRESS_BAR/main_full_add_multithread_progress.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 36 | 36 | 100.0% |
Branches: | 35 | 35 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <iostream> | ||
8 | #include <string> | ||
9 | #include <chrono> | ||
10 | #include <thread> | ||
11 | #include <sstream> | ||
12 | #include <vector> | ||
13 | |||
14 | #include "phoenix_assert.h" | ||
15 | #include "PMultiThreadProgress.h" | ||
16 | |||
17 | ///Fake data computing | ||
18 | /** @param progress : multiple progress bar | ||
19 | * @param vecComputeData : vector of compute data | ||
20 | * @param threadIndex : index of the thread | ||
21 | */ | ||
22 | 1 | void fake_compute_data(PMultiThreadProgress & progress, const std::vector<int> & vecComputeData, size_t threadIndex){ | |
23 | //Simulate computation which depends on input data | ||
24 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
|
4 | for(std::vector<int>::const_iterator it(vecComputeData.begin()); it != vecComputeData.end(); ++it){ |
25 | 3 | size_t computeData(*it); | |
26 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::stringstream name; |
27 |
4/4✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
|
3 | name << "Some computing of thread index " << threadIndex << " compute data " << computeData; |
28 | //Start the progress bar | ||
29 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
|
3 | size_t indexProgressBar = progress.addProgressBar(name.str(), computeData); |
30 | |||
31 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
|
15 | for(size_t i(0lu); i < computeData; ++i){ //Simulate some avancement |
32 |
1/1✓ Branch 1 taken 12 times.
|
12 | progress.incrementProgress(indexProgressBar); //Update the progress bar |
33 |
1/1✓ Branch 2 taken 12 times.
|
12 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); //Simulate computation |
34 | } | ||
35 | //Say the computation is finished | ||
36 |
1/1✓ Branch 1 taken 3 times.
|
3 | progress.incrementProgress(indexProgressBar); |
37 | 3 | } | |
38 | 1 | } | |
39 | |||
40 | 1 | int main(){ | |
41 | 1 | size_t nbValuePerThread(3lu), nbThread(1lu); | |
42 | 1 | size_t nbValue(nbValuePerThread*nbThread); | |
43 | 1 | std::vector<std::vector<int> > vecComputeData; | |
44 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbThread; ++i){ |
45 | 1 | std::vector<int> vecDataPerThread; | |
46 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for(size_t j(0lu); j < nbValuePerThread; ++j){ |
47 | 3 | size_t index(i*nbValuePerThread + j); | |
48 |
1/1✓ Branch 1 taken 3 times.
|
3 | vecDataPerThread.push_back(3lu + ((index*11lu)%5lu)); |
49 | } | ||
50 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecComputeData.push_back(vecDataPerThread); |
51 | 1 | } | |
52 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cout << "Start computation" << std::endl; |
53 | |||
54 |
1/1✓ Branch 1 taken 1 times.
|
1 | PMultiThreadProgress progress(nbValue); |
55 | |||
56 | 1 | std::vector<std::thread> vecThread; | |
57 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecThread.resize(nbThread); |
58 | |||
59 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::thread threadProgress(phoenix_print_parallel_progress, std::ref(progress), 1); |
60 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbThread; ++i){ |
61 |
1/1✓ Branch 3 taken 1 times.
|
1 | vecThread[i] = std::thread(fake_compute_data, std::ref(progress), vecComputeData[i], i); |
62 | } | ||
63 |
1/1✓ Branch 1 taken 1 times.
|
1 | threadProgress.join(); |
64 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(size_t i(0lu); i < nbThread; ++i){ |
65 |
1/1✓ Branch 2 taken 1 times.
|
1 | vecThread[i].join(); |
66 | } | ||
67 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | std::cout << "Done" << std::endl; |
68 | 1 | return 0; | |
69 | 1 | } | |
70 | |||
71 | |||
72 |