Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / examples / sthread / pthread-mutex-simple.c
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* Simple test code with no bug  */
7
8 #include <pthread.h>
9 #include <stdio.h>
10
11 pthread_mutex_t mutex;
12
13 static void* thread_fun(void* val)
14 {
15   pthread_mutex_lock(&mutex);
16   pthread_mutex_unlock(&mutex);
17
18   fprintf(stderr, "The thread %d is terminating.\n", *(int*)val);
19   return NULL;
20 }
21
22 int main(int argc, char* argv[])
23 {
24   pthread_mutex_init(&mutex, NULL);
25
26   int id[2] = {0, 1};
27   pthread_t thread1;
28   pthread_t thread2;
29   pthread_create(&thread1, NULL, thread_fun, &id[0]);
30   pthread_create(&thread2, NULL, thread_fun, &id[1]);
31   fprintf(stderr, "All threads are started.\n");
32   pthread_join(thread1, NULL);
33   pthread_join(thread2, NULL);
34
35   pthread_mutex_destroy(&mutex);
36
37   fprintf(stderr, "User's main is terminating.\n");
38   return 0;
39 }