-
Notifications
You must be signed in to change notification settings - Fork 2
/
mytestcond.c
48 lines (40 loc) · 1019 Bytes
/
mytestcond.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
#include <mythread.h>
#include <mymutex.h>
#include <mycond.h>
#include <limits.h>
#define NTHREADS 100
mythread_cond_t cond;
mythread_mutex_t mut;
void *fun(void *arg)
{
int i;
mythread_mutex_lock(&mut);
printf("Will now wait\n"); fflush(stdout);
mythread_cond_wait(&cond, &mut);
for(i = 0; i < INT_MAX/10; i++);
printf("Exiting \n"); fflush(stdout);
mythread_mutex_unlock(&mut);
mythread_exit(NULL);
return NULL;
}
int main()
{
int i;
mythread_t thread[NTHREADS];
mythread_setconcurrency(NTHREADS+1);
mythread_mutex_init(&mut, NULL);
mythread_cond_init(&cond, NULL);
for(i = 0; i < NTHREADS; i++)
mythread_create(&thread[i], NULL, fun, NULL);
for(i = 0; i < INT_MAX/10; i++);
printf("Broadcast\n"); fflush(stdout);
mythread_cond_broadcast(&cond);
printf("Now join\n"); fflush(stdout);
for(i = 0; i < NTHREADS; i++)
mythread_join(thread[i], NULL);
printf("main exit\n"); fflush(stdout);
mythread_exit(NULL);
mythread_cond_destroy(&cond);
mythread_mutex_destroy(&mut);
return 1;
}