// Compile using gcc gibberish.c -lpthread #include #include #include typedef enum {false, true} bool; pthread_mutex_t* lock; struct info { int tid; const char* message; }; struct info* make_info(int id, const char* message) { struct info* i = malloc(sizeof(struct info)); i->tid = id; i->message = message; return i; } void delay(int n) { int x = 1000000*n; while ( x --> 0 ) { n = x & n; } } void putline(const char* message) { while (*message) { putchar(*message++); delay(1); } putchar('\n'); delay(1); } void* printer(void* input) { struct info* info = input; int i; int tid = info->tid; for (i = 0; i < 100; ++i) { putline(info->message); } free(info); return NULL; } int main() { const char* m[] = { "This line is not written in gibberish", "We want every line to be perfectly readable", "The quick brown fox jumps over lazy dog", "Lorem ipsum dolor sit amet", NULL }; #define N (sizeof(m)/sizeof(*m)-1) // const int N = sizeof(m)/sizeof(*m)-1; int i; pthread_t t[N]; lock = malloc(sizeof(pthread_mutex_t)*N); for (i = 0; i < N; ++i) { pthread_mutex_init(&lock[i], NULL); } for (i = 0; i < N; ++i) { pthread_create(&t[i], NULL, printer, make_info(i, m[i])); } for (i = 0; i < N; ++i) { pthread_join(t[i], NULL); } for (i = 0; i < N; ++i) { pthread_mutex_destroy(&lock[i]); } free(lock); return 0; }