Download c source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
 * Creates a secondary thread that greets in the standard output
 */

#include <pthread.h>
#include <stdio.h>

void* run(void* data) {
  pthread_mutex_t* stdout_mutex = (pthread_mutex_t*)data;
  pthread_mutex_lock(stdout_mutex);
  printf("Hello from secondary thread, mutex at %p\n", data);
  pthread_mutex_unlock(stdout_mutex);

  return NULL;
}

int main(void) {
  pthread_t thread;
  pthread_mutex_t stdout_mutex;
  int error = pthread_mutex_init(&stdout_mutex, /*attr*/ NULL);
  if (error == 0) {
    error = pthread_create(&thread, NULL, run, &stdout_mutex);
    if (error == 0) {
      pthread_mutex_lock(&stdout_mutex);
      printf("Hello from main thread\n");
      pthread_mutex_unlock(&stdout_mutex);
      pthread_join(thread, NULL);
    } else {
      fprintf(stderr, "error: could not create a secondary thread\n");
    }
    pthread_mutex_destroy(&stdout_mutex);
  } else {
    fprintf(stderr, "error: could not init mutex\n");
  }
  return 0;
}

// lock == mutex: mutual exclusion