forked from cloudwu/simplethread
/
test.c
56 lines (51 loc) · 950 Bytes
/
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
#include "simplethread.h"
#include <stdio.h>
#define THREADS 30
struct args {
int n;
struct thread_event *wait;
struct thread_event *trigger;
};
static void
func(void * p) {
struct args * arg = p;
if (arg->wait) {
thread_event_wait(arg->wait);
}
printf("n = %d\n", arg->n);
if (arg->trigger) {
thread_event_trigger(arg->trigger);
}
}
int
main() {
struct thread t[THREADS];
struct args args[THREADS];
struct thread_event ev[THREADS];
int i;
for (i=0;i<THREADS;i++) {
thread_event_create(&ev[i]);
printf("create %d %p\n",i,&ev[i]);
t[i].func = func;
t[i].ud = &args[i];
args[i].n = i;
if (i+1<THREADS) {
args[i].wait = &ev[i];
} else {
args[i].wait = NULL;
}
if (i-1 >=0) {
args[i].trigger = &ev[i-1];
} else {
args[i].trigger = NULL;
}
}
for(i=0;i<THREADS;i++) {
thread_creat(&(t[i]));
}
thread_join(t, THREADS);
for (i=0;i<THREADS;i++) {
thread_event_release(&ev[i]);
}
return 0;
}