Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print numbers in sequence using thread synchronization in C Program.
In C programming, thread synchronization is used to coordinate the execution of multiple threads to achieve a desired sequence. This program demonstrates how to print numbers from 1 to 10 in sequence using two threads − one for even numbers and one for odd numbers.
What is a Thread?
A thread is a lightweight process that runs inside a program. A single program can contain multiple threads executing concurrently. Unlike Java, C does not have built-in multithreading support. Instead, it relies on POSIX Threads (Pthreads) library for multithreading functionality.
Syntax
pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); pthread_join(pthread_t thread, void **retval); pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_unlock(pthread_mutex_t *mutex); pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); pthread_cond_signal(pthread_cond_t *cond);
Note: To compile this program, you need to link the pthread library using: gcc -pthread filename.c
How Thread Synchronization Works
The program uses mutex locks and condition variables to synchronize two threads. One thread prints odd numbers (1, 3, 5...) and another prints even numbers (2, 4, 6...). The synchronization ensures they alternate properly to print numbers in sequence from 1 to 10.
Example
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg) {
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 0) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
void *odd(void *arg) {
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 1) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
int main() {
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&thr, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&thread1, NULL, &odd, NULL);
pthread_create(&thread2, NULL, &even, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&thr);
pthread_cond_destroy(&cond);
return 0;
}
Output
1 2 3 4 5 6 7 8 9
Key Points
-
Mutex Lock: Ensures only one thread can access the shared variable
countat a time. - Condition Variable: Allows threads to wait for specific conditions and signal each other.
-
Thread Creation:
pthread_create()creates new threads that execute specified functions. -
Thread Joining:
pthread_join()waits for threads to complete execution.
Conclusion
Thread synchronization in C using pthread library allows precise control over thread execution order. This example demonstrates how mutex locks and condition variables can coordinate multiple threads to produce sequential output.
