]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/plr/ex/pthread_mutex/main.c
update: sync
[l4.git] / l4 / pkg / plr / ex / pthread_mutex / main.c
1 #include <stdio.h>
2 #include <assert.h>
3
4 #include <pthread.h>
5 #include <pthread-l4.h>
6
7 #include <l4/re/env.h>
8 #include <l4/sys/kdebug.h>
9
10
11 static int globalcounter;
12 pthread_mutex_t mtx;
13
14 static
15 void *thread(void *data)
16 {
17         (void)data;
18         while (1) {
19                 for (unsigned i = 0; i < 10000; ++i) {
20                         pthread_mutex_lock(&mtx);
21                         globalcounter++;
22                         pthread_mutex_unlock(&mtx);
23                 }
24                 printf("\033[31mThread: %d\n", globalcounter);
25         }
26         return NULL;
27 }
28
29 int main(int argc, char **argv)
30 {
31         (void)argc; (void)argv;
32
33         pthread_t pt;
34         pthread_mutex_init(&mtx, 0);
35
36         int res = pthread_create(&pt, NULL, thread, NULL);
37         assert(res == 0);
38
39         while (1) {
40                 for (unsigned i = 0; i < 10000; ++i) {
41                         pthread_mutex_lock(&mtx);
42                         globalcounter++;
43                         pthread_mutex_unlock(&mtx);
44                 }
45                 printf("\033[32mMain: %d\n", globalcounter);
46         }
47
48         pthread_join(pt, NULL);
49
50         enter_kdebug("before return");
51
52         return 0;
53 }