Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement pthread_barrier calls in sthread, and test them in McMini
[simgrid.git] / teshsuite / mc / mcmini / simple_barrier_with_threads_deadlock.c
1 #include <pthread.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 int THREAD_NUM; 
6 int DEBUG = 0;
7
8 pthread_barrier_t barrier;
9 pthread_t *thread;
10
11 static void * thread_doit(void *unused)
12 {
13     if(DEBUG) printf("Thread %lu: Waiting at barrier\n", (unsigned long)pthread_self());
14     pthread_barrier_wait(&barrier);
15     if(DEBUG) printf("Thread %lu: Passed the barrier\n", (unsigned long)pthread_self());
16     return NULL;
17 }
18
19 int main(int argc, char* argv[]) {
20     if(argc != 3){
21         printf("Usage: %s THREAD_NUM DEBUG_FLAG\n", argv[0]);
22         return 1;
23     }
24
25     THREAD_NUM = atoi(argv[1]);
26     DEBUG = atoi(argv[2]);
27
28     thread = (pthread_t*) malloc(THREAD_NUM * sizeof(pthread_t));
29
30     pthread_barrier_init(&barrier, NULL, THREAD_NUM);
31     for(int i = 0; i < THREAD_NUM; i++) {
32         pthread_create(&thread[i], NULL, &thread_doit, NULL);
33     }
34
35     pthread_barrier_wait(&barrier);
36
37     for(int i = 0; i < THREAD_NUM; i++) {
38         pthread_join(thread[i], NULL);
39     }
40
41     free(thread);
42     return 0;
43 }