-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconcurrent_example.cpp
More file actions
53 lines (44 loc) · 1.24 KB
/
concurrent_example.cpp
File metadata and controls
53 lines (44 loc) · 1.24 KB
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
#include <iostream>
#include <unistd.h>
#include <thread>
#include "progresscpp/ProgressBar.hpp"
/*
* Function which is thrown as thread
*/
void workerThread(int total, progresscpp::ProgressBar& progressBar) {
for (int i = 0; i < total; i++) {
++progressBar; // record the tick
usleep(200); // simulate work
// display the bar only at certain steps
if (i % 10 == 0)
progressBar.display();
}
}
/* Example usage of ProgressBar by multiple threads */
int main() {
const int partial = 1000;
/*
* Define a progress bar that has a total of 10000,
* a width of 70, shows `#` to indicate completion
* and a dash '-' for incomplete
*/
progresscpp::ProgressBar progressBar(10 * partial, 70, '#', '-');
/*
* Throw ten threads which access the same ProgressBar
* instance concurrently
*/
std::thread workers[10];
for (int i = 0; i < 10; i++) {
workers[i] = std::thread(workerThread, partial, std::ref(progressBar));
}
/*
* Wait for threads to finish the work
*/
for (int i = 0; i < 10; i++) {
workers[i].join();
}
// tell the bar to finish
progressBar.done();
std::cout << "Done!" << std::endl;
return 0;
}