Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add more Mc Mini tests
[simgrid.git] / teshsuite / mc / mcmini / simple_semaphores_with_threads_ok.c
1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 sem_t sem1, sem2;
7 pthread_t thread1, thread2;
8
9 static void * thread1_doit(void *unused) {
10     sem_wait(&sem2);
11     sem_post(&sem1);
12     sem_wait(&sem2);
13     return NULL;
14 }
15
16 static void * thread2_doit(void *sem_count) {
17     int start_num = *((int*)sem_count);
18     for(int i = 0; i < start_num + 1; i++) {
19         sem_wait(&sem1);
20     }
21     sem_post(&sem2);
22     return NULL;
23 }
24
25 int main(int argc, char* argv[]) {
26     if(argc < 2) {
27         printf("Expected usage: %s START_NUM\n", argv[0]);
28         return -1;
29     }
30
31     int start_num = atoi(argv[1]);
32
33     sem_init(&sem1, 0, start_num);
34     sem_init(&sem2, 0, 1);
35
36     pthread_create(&thread1, NULL, &thread1_doit, NULL);
37     pthread_create(&thread2, NULL, &thread2_doit, &start_num);
38
39     pthread_join(thread1, NULL);
40     pthread_join(thread2, NULL);
41
42     sem_destroy(&sem1);
43     sem_destroy(&sem2);
44
45     return 0;
46 }