-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.c
More file actions
69 lines (52 loc) · 1.13 KB
/
test.c
File metadata and controls
69 lines (52 loc) · 1.13 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include "progress.h"
#define SEC *1000000
#define NUM_PROGS 20
char titles[NUM_PROGS][10];
void * thread_fun(void * b) {
Progressbar * bar = b;
while (bar->progress < bar->limit) {
++bar->progress;
usleep(0.001 SEC);
}
}
int main() {
srand(time(0));
pthread_t threads[NUM_PROGS];
Progressbar pb[NUM_PROGS];
ProgressInfo pinfo;
pinfo.progressbars = pb;
pinfo.num_progressbars = NUM_PROGS;
pinfo.fill_char = '=';
pinfo.last_char = '>';
for (Progressbar * bar = pb; bar < pb + NUM_PROGS; ++bar) {
bar->limit = rand() % 10000;
bar->progress = 0;
bar->show_percentage = 1;
}
for (int i = 0; i < NUM_PROGS; ++i) {
sprintf(titles[i], "Bar %d", i);
pb[i].title = titles[i];
}
init_bars(&pinfo);
for (int i = 0; i < NUM_PROGS; ++i) {
pthread_create(threads + i, NULL, thread_fun, (void *)(pb + i));
}
int done;
do {
usleep(0.001 SEC);
done = 1;
for (int i = 0; i < NUM_PROGS; ++i) {
if (pb[i].progress < pb[i].limit) {
done = 0;
break;
}
}
print_bars(&pinfo);
} while (!done);
return 0;
}