Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Please valgrind.
[simgrid.git] / examples / sthread / pthread-mutex-simple.c
1 /* Simple test code with no bug  */
2
3 #include <pthread.h>
4 #include <stdio.h>
5
6 pthread_mutex_t mutex;
7
8 static void* thread_fun(void* val)
9 {
10   pthread_mutex_lock(&mutex);
11   pthread_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   pthread_mutex_init(&mutex, NULL);
20
21   int id[2] = {0, 1};
22   pthread_t thread1;
23   pthread_t thread2;
24   pthread_create(&thread1, NULL, thread_fun, (void*)&id[0]);
25   pthread_create(&thread2, NULL, thread_fun, (void*)&id[1]);
26   fprintf(stderr, "All threads are started.\n");
27   pthread_join(thread1, NULL);
28   pthread_join(thread2, NULL);
29
30   pthread_mutex_destroy(&mutex);
31
32   fprintf(stderr, "User's main is terminating.\n");
33   return 0;
34 }