|
| 1 | +/* |
| 2 | + * Project 18: Multithreading with POSIX Threads (pthreads) |
| 3 | + * Author: Bocaletto Luca |
| 4 | + * Date: 2025-06-22 |
| 5 | + * GitHub: bocaletto-luca |
| 6 | + * |
| 7 | + * Description: |
| 8 | + * This program spawns multiple threads that concurrently increment a shared counter. |
| 9 | + * A mutex is used to protect the shared variable from race conditions. Each thread |
| 10 | + * performs a fixed number of increments, and the final counter value is printed after |
| 11 | + * all threads have completed their work. |
| 12 | + * |
| 13 | + * Key Concepts: |
| 14 | + * - Creating threads using pthread_create(). |
| 15 | + * - Synchronizing threads with pthread_mutex for safe access to shared data. |
| 16 | + * - Joining threads with pthread_join() to ensure all threads complete before proceeding. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <pthread.h> |
| 22 | + |
| 23 | +#define NUM_THREADS 5 |
| 24 | +#define INCREMENTS_PER_THREAD 100000 |
| 25 | + |
| 26 | +// Global shared counter that will be incremented by multiple threads. |
| 27 | +long counter = 0; |
| 28 | + |
| 29 | +// Mutex to protect access to the shared 'counter' variable. |
| 30 | +pthread_mutex_t counter_mutex; |
| 31 | + |
| 32 | +/* |
| 33 | + * Function: threadFunction |
| 34 | + * ------------------------ |
| 35 | + * Each thread executes this function. It loops for a fixed number of increments, |
| 36 | + * and in each iteration, it locks the mutex, increments the counter, and then unlocks |
| 37 | + * the mutex to allow other threads to access the shared counter. |
| 38 | + * |
| 39 | + * arg: A pointer to any argument we want to pass (unused in this example). |
| 40 | + * |
| 41 | + * returns: NULL upon completion. |
| 42 | + */ |
| 43 | +void *threadFunction(void *arg) { |
| 44 | + for (int i = 0; i < INCREMENTS_PER_THREAD; i++) { |
| 45 | + // Lock the mutex before updating the shared counter. |
| 46 | + pthread_mutex_lock(&counter_mutex); |
| 47 | + counter++; // Critical section: update shared variable. |
| 48 | + // Unlock the mutex to allow other threads access to the counter. |
| 49 | + pthread_mutex_unlock(&counter_mutex); |
| 50 | + } |
| 51 | + return NULL; |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Function: main |
| 56 | + * -------------- |
| 57 | + * Sets up the multithreading environment by initializing the mutex, creating threads, |
| 58 | + * waiting for them to finish execution, and then cleaning up before exiting. |
| 59 | + */ |
| 60 | +int main(void) { |
| 61 | + pthread_t threads[NUM_THREADS]; |
| 62 | + int i, status; |
| 63 | + |
| 64 | + // Initialize the mutex with default attributes. |
| 65 | + if (pthread_mutex_init(&counter_mutex, NULL) != 0) { |
| 66 | + printf("Error initializing mutex.\n"); |
| 67 | + exit(EXIT_FAILURE); |
| 68 | + } |
| 69 | + |
| 70 | + // Create multiple threads. |
| 71 | + for (i = 0; i < NUM_THREADS; i++) { |
| 72 | + status = pthread_create(&threads[i], NULL, threadFunction, NULL); |
| 73 | + if (status != 0) { |
| 74 | + printf("Error creating thread %d.\n", i); |
| 75 | + exit(EXIT_FAILURE); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Wait for all threads to finish. |
| 80 | + for (i = 0; i < NUM_THREADS; i++) { |
| 81 | + status = pthread_join(threads[i], NULL); |
| 82 | + if (status != 0) { |
| 83 | + printf("Error joining thread %d.\n", i); |
| 84 | + exit(EXIT_FAILURE); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Destroy the mutex after use. |
| 89 | + pthread_mutex_destroy(&counter_mutex); |
| 90 | + |
| 91 | + // Calculate the expected counter value. |
| 92 | + long expected = NUM_THREADS * INCREMENTS_PER_THREAD; |
| 93 | + printf("Final counter value: %ld (expected: %ld)\n", counter, expected); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments