forked from ambrop72/badvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadwork_test.c
87 lines (68 loc) · 1.76 KB
/
threadwork_test.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stddef.h>
#include <stdio.h>
#include <misc/debug.h>
#include <base/BLog.h>
#include <base/DebugObject.h>
#include <threadwork/BThreadWork.h>
BReactor reactor;
BThreadWorkDispatcher twd;
BThreadWork tw1;
BThreadWork tw2;
BThreadWork tw3;
int num_left;
static void handler_done (void *user)
{
printf("work done\n");
num_left--;
if (num_left == 0) {
printf("all works done, quitting\n");
BReactor_Quit(&reactor, 0);
}
}
static void work_func (void *user)
{
unsigned int x = 0;
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
x++;
}
}
}
static void dummy_works (int n)
{
for (int i = 0; i < n; i++) {
BThreadWork tw_tmp;
BThreadWork_Init(&tw_tmp, &twd, handler_done, NULL, work_func, NULL);
BThreadWork_Free(&tw_tmp);
}
}
int main ()
{
BLog_InitStdout();
BLog_SetChannelLoglevel(BLOG_CHANNEL_BThreadWork, BLOG_DEBUG);
if (!BReactor_Init(&reactor)) {
DEBUG("BReactor_Init failed");
goto fail1;
}
if (!BThreadWorkDispatcher_Init(&twd, &reactor, 1)) {
DEBUG("BThreadWorkDispatcher_Init failed");
goto fail2;
}
dummy_works(200);
BThreadWork_Init(&tw1, &twd, handler_done, NULL, work_func, NULL);
BThreadWork_Init(&tw2, &twd, handler_done, NULL, work_func, NULL);
BThreadWork_Init(&tw3, &twd, handler_done, NULL, work_func, NULL);
dummy_works(200);
num_left = 3;
BReactor_Exec(&reactor);
BThreadWork_Free(&tw3);
BThreadWork_Free(&tw2);
BThreadWork_Free(&tw1);
BThreadWorkDispatcher_Free(&twd);
fail2:
BReactor_Free(&reactor);
fail1:
BLog_Free();
DebugObjectGlobal_Finish();
return 0;
}