/* * C (multithreaded) using POSIX Thread Library (pthread) * by Stefan Scheler * Ilmenau, Germany - May 2005 * * compile with: gcc -pthread -o 99bottles 99bottles.c * disable debug output with: ./99bottles 2>/dev/null * */ #include #include #include #define THREADS 10 #define PLURALS (bottles>1) ? "s" : "" pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int bottles = 99; void *takeonedownandpassitaround(void *ptr) { struct timespec sleeptime; int *thread_id = (int *)ptr; while (1) { pthread_mutex_lock(&mutex); if (bottles>0) { fprintf(stderr, "Thread %d: ", *thread_id); printf("%d bottle%s of beer on the wall, %d bottle%s of beer.\n", bottles, PLURALS, \