Download c source code

/*
 * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
 * Creates a secondary thread that greets in the standard output
 */

#include <inttypes.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct shared_thread_data {
  size_t thread_count;
  sem_t* can_print;
  pthread_mutex_t stdout_mutex;
} shared_thread_data_t;

typedef struct private_thread_data {
  size_t thread_number;  // rank
  shared_thread_data_t* shared_thread_data;
} private_thread_data_t;

void* run(void* data);
int create_threads(shared_thread_data_t* shared_thread_data);

int main(int argc, char* argv[]) {
  int error = 0;

  size_t thread_count = sysconf(_SC_NPROCESSORS_ONLN);
  if (argc >= 2) {
    thread_count = strtoull(argv[1], NULL, 10);
  }

  shared_thread_data_t* shared_thread_data = (shared_thread_data_t*)
    calloc(1, sizeof(shared_thread_data_t));

  if (shared_thread_data) {
    shared_thread_data->thread_count = thread_count;
    shared_thread_data->can_print = (sem_t*)calloc(thread_count, sizeof(sem_t));
    if (shared_thread_data->can_print) {
      error = pthread_mutex_init(&shared_thread_data->stdout_mutex
        , /*attr*/NULL);

      if (error == 0) {
        struct timespec start_time, finish_time;
        clock_gettime(/*clk_id*/CLOCK_MONOTONIC, &start_time);

        error = create_threads(shared_thread_data);

        clock_gettime(/*clk_id*/CLOCK_MONOTONIC, &finish_time);
        double elapsed_time = finish_time.tv_sec - start_time.tv_sec +
          (finish_time.tv_nsec - start_time.tv_nsec) * 1e-9;
        printf("execution time: %.9lfs\n", elapsed_time);

        pthread_mutex_destroy(&shared_thread_data->stdout_mutex);
      } else {
        fprintf(stderr, "error: could not init mutex\n");
        error = 11;
      }

      free(shared_thread_data->can_print);
    } else {
      fprintf(stderr, "error: could not allocated shared memory\n");
      error = 13;
    }

    free(shared_thread_data);
  } else {
    fprintf(stderr, "error: could not allocated shared memory\n");
    error = 12;
  }

  return error;
}

int create_threads(shared_thread_data_t* shared_thread_data) {
  int error = 0;
  pthread_t* threads = (pthread_t*)
    malloc(shared_thread_data->thread_count * sizeof(pthread_t));

  private_thread_data_t* private_thread_data = (private_thread_data_t*)
    calloc(shared_thread_data->thread_count, sizeof(private_thread_data_t));

  if (threads && private_thread_data) {
    for (size_t index = 0; index < shared_thread_data->thread_count; ++index) {
      /*error =*/sem_init(&shared_thread_data->can_print[index], /*pshared*/0
        , /*value*/ !index);
      private_thread_data[index].thread_number = index;
      private_thread_data[index].shared_thread_data = shared_thread_data;

      error = pthread_create(&threads[index], NULL, run
        , &private_thread_data[index]);

      if (error) {
        fprintf(stderr, "error: could not create thread %zu\n", index);
        error = 21;
      }
    }

    // pthread_mutex_lock(&shared_thread_data->stdout_mutex);
    // printf("Hello from main thread\n");
    // pthread_mutex_unlock(&shared_thread_data->stdout_mutex);

    for (size_t index = 0; index < shared_thread_data->thread_count; ++index) {
      pthread_join(threads[index], NULL);
      sem_destroy(&shared_thread_data->can_print[index]);
    }

    free(private_thread_data);
    free(threads);
  } else {
    fprintf(stderr, "error: could not allocate memory for %zu threads\n"
      , shared_thread_data->thread_count);
    error = 22;
  }

  return error;
}

void* run(void* data) {
  private_thread_data_t* private_data = (private_thread_data_t*)data;
  shared_thread_data_t *shared_data = private_data->shared_thread_data;

  sem_wait(&shared_data->can_print[private_data->thread_number]);

  // Do the ordered-task here
  // pthread_mutex_lock(&private_data->shared_thread_data->stdout_mutex);
  printf("Hello from secondary thread %zu of %zu\n"
    , (*private_data).thread_number, shared_data->thread_count);
  // pthread_mutex_unlock(&shared_data->stdout_mutex);

  // Allow subsequent thread to do the task
  sem_post(&shared_data->can_print[(private_data->thread_number + 1)
    % shared_data->thread_count]);

  return NULL;
}