forked from stephenmathieson/batch.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
53 lines (43 loc) · 882 Bytes
/
example.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
#include <assert.h>
#include <stdio.h>
#include <pthread.h> // pthread_self
#include <unistd.h> // sleep
#include "batch.h"
int count = 0;
int
is_even(int n) {
return 0 == n % 2 ? 1 : 0;
}
void *
function_even(void *arg) {
int n = (int) arg;
assert(is_even(n));
printf("thread 0x%x: EVEN <--> %d\n", (int) pthread_self(), n);
count++;
sleep(1);
return NULL;
}
void *
function_odd(void *arg) {
int n = (int) arg;
assert(0 == is_even(n));
printf("thread 0x%x: ODD <--> %d\n", (int) pthread_self(), n);
count++;
sleep(1);
return NULL;
}
int
main() {
void *batch = batch_new(4);
assert(batch);
for (int i = 0; i < 20; ++i) {
void *(*function)(void *) = is_even(i)
? function_even
: function_odd;
batch_push(batch, function, (void *) i);
}
batch_wait(batch);
batch_end(batch);
assert(20 == count);
return 0;
}