Print 1 2 3 infinitely using threads in C

In C programming, we can print the sequence "1 2 3" infinitely using multiple threads with proper synchronization. This demonstrates thread coordination using mutexes and condition variables to ensure orderly execution.

Syntax

pthread_create(&thread_id, NULL, thread_function, argument);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&condition, &mutex);
pthread_cond_signal(&condition);
pthread_mutex_unlock(&mutex);

Installation: On Linux systems, compile with: gcc filename.c -lpthread

Example

This program creates three threads that print numbers 1, 2, and 3 in sequence infinitely. Each thread waits for its turn using condition variables −

#include <stdio.h>
#include <pthread.h>

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 value = 1;

void *printNumber(void *n) {
    int num = *(int*)n;
    
    while(1) {
        pthread_mutex_lock(&lock);
        
        /* Wait until it's this thread's turn */
        if (value != num) {
            if (num == 1) {
                pthread_cond_wait(&cond1, &lock);
            } else if (num == 2) {
                pthread_cond_wait(&cond2, &lock);
            } else {
                pthread_cond_wait(&cond3, &lock);
            }
        }
        
        /* Print the number */
        printf("%d ", num);
        
        /* Update value and signal next thread */
        if (value == 3) {
            value = 1;
            pthread_cond_signal(&cond1);
        } else if (value == 1) {
            value = 2;
            pthread_cond_signal(&cond2);
        } else if (value == 2) {
            value = 3;
            pthread_cond_signal(&cond3);
        }
        
        pthread_mutex_unlock(&lock);
    }
    return NULL;
}

int main() {
    pthread_t thread1, thread2, thread3;
    int n1 = 1, n2 = 2, n3 = 3;
    
    /* Create three threads */
    pthread_create(&thread1, NULL, printNumber, (void *)&n1);
    pthread_create(&thread2, NULL, printNumber, (void *)&n2);
    pthread_create(&thread3, NULL, printNumber, (void *)&n3);
    
    /* Let threads run infinitely */
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    
    return 0;
}
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3...

How It Works

  • Mutex: Ensures only one thread accesses the shared variable value at a time
  • Condition Variables: Each thread waits on its specific condition variable until signaled
  • Sequential Execution: Threads print in order 1?2?3?1... by checking the value variable
  • Infinite Loop: The cycle continues indefinitely as each thread signals the next one

Key Points

  • Use pthread_mutex_lock() and pthread_mutex_unlock() to protect shared data
  • pthread_cond_wait() blocks the thread until the condition is signaled
  • Always compile with -lpthread flag when using POSIX threads
  • The program runs infinitely − use Ctrl+C to terminate

Conclusion

This example demonstrates thread synchronization in C using mutexes and condition variables. The coordinated approach ensures that multiple threads print numbers in a specific sequence without race conditions.

Updated on: 2026-03-15T12:44:08+05:30

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements