Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enable simple sthread example.
[simgrid.git] / examples / sthread / sthread-mutex-simple.c
1 /* Simple test code with no bug  */
2
3 #include "src/sthread/sthread.h"
4 #include <stdio.h>
5
6 sthread_mutex_t mutex;
7
8 static void* thread_fun(void* val)
9 {
10   sthread_mutex_lock(&mutex);
11   sthread_mutex_unlock(&mutex);
12
13   fprintf(stderr, "The thread %d is terminating.\n", *(int*)val);
14   return NULL;
15 }
16
17 int main(int argc, char* argv[])
18 {
19   sthread_mutex_init(&mutex, NULL);
20
21   int id[2] = {0, 1};
22   sthread_t thread1;
23   sthread_t thread2;
24   sthread_create(&thread1, NULL, thread_fun, (void*)&id[0]);
25   sthread_create(&thread2, NULL, thread_fun, (void*)&id[1]);
26   fprintf(stderr, "All threads are started.\n");
27   sthread_join(thread1, NULL);
28   sthread_join(thread2, NULL);
29
30   sthread_mutex_destroy(&mutex);
31
32   fprintf(stderr, "User's main is terminating.\n");
33   return 0;
34 }