Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add more Mc Mini tests
[simgrid.git] / teshsuite / mc / mcmini / simple_mutex_with_threads_ok.c
1 #include <pthread.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 pthread_mutex_t mutex1;
6 pthread_mutex_t mutex2;
7
8 static void * thread_doit(void *unused) {
9     pthread_mutex_lock(&mutex1);
10     pthread_mutex_lock(&mutex2);
11     pthread_mutex_unlock(&mutex2);
12     pthread_mutex_unlock(&mutex1);
13     return NULL;
14 }
15
16 int main(int argc, char* argv[]) {
17
18     if(argc < 2) {
19         printf("Expected usage: %s THREAD_NUM\n", argv[0]);
20         return -1;
21     }
22
23     int THREAD_NUM = atoi(argv[1]);
24
25     if(THREAD_NUM < 2) {
26         printf("At least 2 threads are required\n");
27         return -1;
28     }
29
30     pthread_t *threads = malloc(sizeof(pthread_t) * THREAD_NUM);
31
32     pthread_mutex_init(&mutex1, NULL);
33     pthread_mutex_init(&mutex2, NULL);
34
35     for(int i = 0; i < THREAD_NUM; i++) {
36         pthread_create(&threads[i], NULL, &thread_doit, NULL);
37     }
38
39     for(int i = 0; i < THREAD_NUM; i++) {
40         pthread_join(threads[i], NULL);
41     }
42
43     free(threads);
44     pthread_mutex_destroy(&mutex1);
45     pthread_mutex_destroy(&mutex2);
46
47     return 0;
48 }