C Concurrency: POSIX Threads (pthreads), Mutexes & Race Conditions Masterclass
Welcome to Phase 22 (Chapter 62): C Concurrency โ POSIX Threads (pthreads), Mutexes & Race Conditions Masterclass! Multithreading enables concurrent execution across multi-core CPUs. In this guide, you will master POSIX threads (`pthreads`), race condition prevention with mutex locks, and deadlock elimination.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 4
#define ITERATIONS 100000
static long counter = 0;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *worker(void *arg) {
for (int i = 0; i < ITERATIONS; i++) {
pthread_mutex_lock(&lock); // CRITICAL SECTION START
counter++; // Safe increment
pthread_mutex_unlock(&lock); // CRITICAL SECTION END
}
return NULL;
}
int main(void) {
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&threads[i], NULL, worker, NULL);
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
printf("Final Synchronized Counter: %ld (Expected: %d)\n",
counter, NUM_THREADS * ITERATIONS);
pthread_mutex_destroy(&lock);
return 0;
} Q1: What is a Race Condition?
Occurs when two or more threads concurrently access shared memory and at least one access is a write, producing non-deterministic bugs.
Q2: What is a Deadlock?
A situation where Thread A holds Lock 1 and waits for Lock 2, while Thread B holds Lock 2 and waits for Lock 1, causing both threads to freeze forever.
Q3: How do you prevent deadlocks in C multithreading?
Always acquire multiple locks in the exact same global lock hierarchy order across all threads in your application.
Q4: What is the difference between pthread_join and pthread_detach?
`pthread_join()` waits for a thread to exit and collects its return value. `pthread_detach()` releases thread resources automatically upon exit.
Q5: What are atomic operations in C11 (<stdatomic.h>)?
Hardware-level atomic instructions (`atomic_fetch_add`) that perform thread-safe variable updates without the overhead of mutex locking.