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

sem_t mutex;
int s = 0;

void* job (void* id) {
	printf("Start thread ID #%d\n", (int) id);
	while(1) {
		//sleep(1); //(1)
		sem_wait(&mutex);
		//sleep(1); //(2)
			printf("Thread #%d is doing math. %d + 1 = %d.\n", (int) id, s, s+1);
			s++;
		//sleep(1);	//(3)
		sem_post(&mutex);
		sleep(1);	//(4)
	}
}

int main() {
	pthread_t thread[10];
	
	int i;
	sem_init(&mutex, 0, 1);
	for (i = 0; i<10; ++i)
		pthread_create(&(thread[i]), NULL, job, (void*) i);
	
	sleep(100);
}
