// ----------------------------------------------------
// Sample pthread program 4 by: Thomas Braunl, UWA 2004
// Have several tasks (prints) per slave iteration
// Requires SYNCHRONIZATION !
// ----------------------------------------------------
#include <pthread.h>
#include <stdio.h>

#define repeats   3
#define threadnum 5

pthread_mutex_t  mutex;

void *slave(void *arg)
{ int id = (int) arg ;

  for (int i = 0; i < repeats; i++) 
  { pthread_mutex_lock(&mutex);
      printf("%d-A\n", id);
      printf("%d-B\n", id);
      printf("%d-C\n", id);  
    pthread_mutex_unlock(&mutex);
  }
  return( 0 ) ;
}


int main()
{ pthread_t  thread;
  
  if (pthread_mutex_init(&mutex, NULL))
    { printf("Error: mutex failed\n"); exit(1); }
  for(int i = 0; i < threadnum ; i++) 
  { if (pthread_create(&thread, NULL, slave, (void *) i)) 
    { printf("Error: thread creation failed\n"); exit(1); }
  }
  pthread_exit( 0 );
}

