In: Computer Science
The following code uses while loop to print 1 2 3 1 2 3 1 2 3 1 2 3 ...... infinitely using multi-threading. Thumbs up if you find the code helpful :)
CODE:
#include <stdio.h>
#include <pthread.h>
// Declaration of thread condition variables
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int done = 1;
void *f(void *n)
{
while(1) {
pthread_mutex_lock(&lock);
if (done != (int)*(int*)n) {
if ((int)*(int*)n == 1) {
pthread_cond_wait(&cond1, &lock);
} else if ((int)*(int*)n == 2) {
pthread_cond_wait(&cond2, &lock);
}
else {
pthread_cond_wait(&cond3, &lock);
}
}
printf("%d ", *(int*)n);
if (done == 3) {
done = 1;
pthread_cond_signal(&cond1);
}
else if(done == 1) {
done = 2;
pthread_cond_signal(&cond2);
} else if (done == 2) {
done = 3;
pthread_cond_signal(&cond3);
}
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main()
{
pthread_t t1, t2, t3;
int n1 = 1, n2 = 2, n3 = 3;
// Creating 3 thread
pthread_create(&t1, NULL, f, (void *)&n1);
pthread_create(&t2, NULL, f, (void *)&n2);
pthread_create(&t3, NULL, f, (void *)&n3);
// infinite loop to avoid exit of program
for(;;);
return 0;
}
OUTPUT: