/*
 * 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 4
#define NUM_INCREMENTS 1000000

static int total = 0;

static void* Count(void* unused) {
  (void) unused;
  for (int index = 0; index < NUM_INCREMENTS; index++) {
    total++;
  }
  return NULL;
}

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

  for (int index = 0; index < NUM_THREADS; index++) {
    if (pthread_create(&threads[index], NULL, Count, NULL) != 0) {
      return EXIT_FAILURE;
    }
  }
  for (int index = 0; index < NUM_THREADS; index++) {
    pthread_join(threads[index], NULL);
  }

  printf("Expected: %d\n", NUM_THREADS * NUM_INCREMENTS);
  printf("Actual:   %d\n", total);
  return EXIT_SUCCESS;
}