aae622bef64b177989b5f91397a2792800d11e27
[c11tester.git] / pthread_test / condvar.cc
1 #include <stdio.h>
2
3 #include "pthread.h"
4 #include "librace.h"
5 #include "stdatomic.h"
6 #include "mutex.h"
7 #include <condition_variable>
8
9 cdsc::mutex * m;
10 cdsc::condition_variable *v;
11 int shareddata;
12
13 static void *a(void *obj)
14 {
15         m->lock();
16         while(load_32(&shareddata)==0)
17                 v->wait(*m);
18         m->unlock();
19         return NULL;
20 }
21
22 static void *b(void *obj)
23 {
24         m->lock();
25         store_32(&shareddata, (unsigned int) 1);
26         v->notify_all();
27         m->unlock();
28         return NULL;
29 }
30
31 int user_main(int argc, char **argv)
32 {
33         pthread_t t1, t2;
34         store_32(&shareddata, (unsigned int) 0);
35         m=new cdsc::mutex();
36         v=new cdsc::condition_variable();
37
38         pthread_create(&t1,NULL, &a, NULL);
39         pthread_create(&t2,NULL, &b, NULL);
40
41         pthread_join(t1,NULL);
42         pthread_join(t2,NULL);
43         return 0;
44 }