1 #include <exec/memory.h>
2 #include <libraries/thread.h>
3 #include <proto/exec.h>
5 #include <proto/thread.h>
14 void *waiter_thread(void *data
) {
15 struct thread_data
*td
= (struct thread_data
*) data
;
16 uint32_t id
= CurrentThread();
18 printf("[%d] starting, locking the mutex\n", id
);
21 printf("[%d] waiting on the condition\n", id
);
22 WaitCondition(td
->cond
, td
->mutex
);
24 printf("[%d] condition signalled, unlocking the mutex\n", id
);
25 UnlockMutex(td
->mutex
);
27 printf("[%d] all done, exiting\n", id
);
32 int main (int argc
, char **argv
) {
33 struct thread_data
*td
;
36 td
= AllocMem(sizeof(struct thread_data
), MEMF_PUBLIC
| MEMF_CLEAR
);
38 printf("creating mutex\n");
39 td
->mutex
= CreateMutex();
41 printf("creating condition\n");
42 td
->cond
= CreateCondition();
44 printf("starting waiter threads\n");
45 for (i
= 0; i
< 5; i
++)
46 CreateThread(waiter_thread
, (void *) td
);
48 printf("sleeping for 2s\n");
51 printf("signalling condition\n");
52 SignalCondition(td
->cond
);
54 printf("sleeping for 2s\n");
57 printf("broadcasting condition\n");
58 BroadcastCondition(td
->cond
);
60 printf("waiting for threads to exit\n");
63 printf("destroying the condition\n");
64 DestroyCondition(td
->cond
);
66 printf("destroying the mutex\n");
67 DestroyMutex(td
->mutex
);
69 FreeMem(td
, sizeof(struct thread_data
));