Certainly! Below is a C program that demonstrates thread synchronization using a shared counter variable. We'll explore two scenarios:
- Without Locks: This version does not use any synchronization mechanisms, leading to race conditions.
- With Locks: This version uses
pthread_mutex to synchronize access to the shared counter, ensuring correct results.
1. Without Locks
Explanation
When multiple threads access and modify a shared variable concurrently without proper synchronization, it can lead to race conditions. In this scenario, each thread attempts to increment the global counter 1000 times. However, without locks, the counter updates may interfere with each other, resulting in a final value less than the expected 10,000.
Code
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 10
#define INCREMENTS 1000
// Global counter variable
int counter = 0;
// Thread function to increment the counter
void* increment_counter(void* arg) {
for(int i = 0; i < INCREMENTS; i++) {
// Increment without any synchronization
counter++;
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
// Create threads
for(int i = 0; i < NUM_THREADS; i++) {
rc = pthread_create(&threads[i], NULL, increment_counter, NULL);
if(rc) {
fprintf(stderr, "Error: Unable to create thread, %d\n", rc);
exit(EXIT_FAILURE);
}
}
// Wait for all threads to complete
for(int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Print the final counter value
printf("Final counter value without locks: %d\n", counter);
pthread_exit(NULL);
}
Compilation and Execution
To compile and run the program:
gcc -pthread -o counter_no_lock counter_no_lock.c
./counter_no_lock
Expected Output
Due to race conditions, the final counter value is typically less than 10000. For example:
Final counter value without locks: 8743
Note: The exact value may vary with each execution.
2. With Locks
Explanation
To prevent race conditions, we can use a mutex (pthread_mutex_t) to ensure that only one thread modifies the counter at a time. This synchronization guarantees that all increments are performed correctly, resulting in the expected final value of 10,000.
Code
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 10
#define INCREMENTS 1000
// Global counter variable
int counter = 0;
// Mutex for synchronizing access to counter
pthread_mutex_t counter_mutex;
// Thread function to increment the counter
void* increment_counter(void* arg) {
for(int i = 0; i < INCREMENTS; i++) {
// Lock the mutex before modifying the counter
pthread_mutex_lock(&counter_mutex);
counter++;
// Unlock the mutex after modification
pthread_mutex_unlock(&counter_mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
// Initialize the mutex
if(pthread_mutex_init(&counter_mutex, NULL) != 0) {
fprintf(stderr, "Error: Mutex initialization failed\n");
exit(EXIT_FAILURE);
}
// Create threads
for(int i = 0; i < NUM_THREADS; i++) {
rc = pthread_create(&threads[i], NULL, increment_counter, NULL);
if(rc) {
fprintf(stderr, "Error: Unable to create thread, %d\n", rc);
exit(EXIT_FAILURE);
}
}
// Wait for all threads to complete
for(int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Destroy the mutex
pthread_mutex_destroy(&counter_mutex);
// Print the final counter value
printf("Final counter value with locks: %d\n", counter);
pthread_exit(NULL);
}
Compilation and Execution
To compile and run the program:
gcc -pthread -o counter_with_lock counter_with_lock.c
./counter_with_lock
Expected Output
With proper synchronization, the final counter value should be exactly 10000:
Final counter value with locks: 10000
Summary
-
Without Locks: The program demonstrates race conditions, leading to an incorrect final counter value (typically less than 10,000).
-
With Locks: Using a mutex to synchronize access ensures that the final counter value is correctly updated to 10,000.
This example highlights the importance of synchronization mechanisms like mutexes in multi-threaded programming to prevent race conditions and ensure data consistency.