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
// Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
// Creates two threads using POSIX Threads that greet in stdout

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

void* run(void* data);

int main(void) {
  pthread_t thread;
  if (pthread_create(&thread, /*attr*/ NULL, run, (void*)13) == EXIT_SUCCESS) {
    printf("Hello from main thread\n");
    pthread_join(thread, /*value_ptr*/ NULL);
    return EXIT_SUCCESS;
  } else {
    fprintf(stderr, "Could not create secundary thread\n");
    return EXIT_FAILURE;
  }
}

void* run(void* data) {
  printf("Hello from secundary thread\n");
  printf("data = %p\n", data);

  return NULL;
}