Merge branch 'branch-weiyu' of ssh://plrg.eecs.uci.edu:/home/git/random-fuzzer into...
[c11tester.git] / pthread_test / protect / mutex_test.cc
1 #include <stdio.h> 
2  
3 #include "threads.h" 
4 #include "librace.h" 
5 #include "stdatomic.h" 
6 #include <pthread.h>
7
8 int shareddata;  
9 pthread_mutex_t m;
10
11 static void* a(void *obj) 
12
13         int i; 
14         for(i=0;i<2;i++) { 
15                 if ((i%2)==0) { 
16                         pthread_mutex_lock(&m);
17                         store_32(&shareddata,(unsigned int)i); 
18                         printf("shareddata: %d\n", shareddata);
19                         pthread_mutex_unlock(&m);
20                 } else { 
21                         while(!pthread_mutex_trylock(&m))
22                                 thrd_yield(); 
23                         store_32(&shareddata,(unsigned int)i); 
24                         printf("shareddata: %d\n", shareddata);
25                         pthread_mutex_unlock(&m);
26                 } 
27         } 
28
29  
30 int user_main(int argc, char **argv) 
31
32         thrd_t t1, t2; 
33         pthread_mutex_init(&m, NULL);
34
35         thrd_create(&t1, (thrd_start_t)&a, NULL);
36         thrd_create(&t2, (thrd_start_t)&a, NULL);
37
38         thrd_join(t1); 
39         thrd_join(t2); 
40         return 0; 
41 }