/*
 * Copyright ©2026 Soham Pardeshi. All rights reserved.
 * Permission is hereby granted to students registered for University of
 * Washington CSE 333 for use solely during Summer Quarter 2026 for
 * purposes of the course. No other use, copying, distribution, or
 * modification is permitted without prior written consent. Copyrights
 * for third-party components of this work must be honored. Instructors
 * interested in reusing these course materials should contact the author.
 */

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

#define NUM_THREADS 5

static void* PrintNumber(void* raw_number) {
  int* number = raw_number;
  printf("%d\n", *number);
  free(number);
  return NULL;
}

int main(void) {
  pthread_t threads[NUM_THREADS];
  int created = 0;

  for (int value = 1; value <= NUM_THREADS; value++) {
    int* number = malloc(sizeof(*number));
    if (number == NULL) {
      break;
    }
    *number = value;

    int rc = pthread_create(&threads[created], NULL, PrintNumber, number);
    if (rc != 0) {
      free(number);
      break;
    }
    created++;
  }

  for (int index = 0; index < created; index++) {
    pthread_join(threads[index], NULL);
  }
  return EXIT_SUCCESS;
}