1. Shared Memory in C (with
Pthreads)
Understanding parallel programming with
shared memory
Number of threads: 4
Level: Intermediate
2. What is Shared Memory?
• Memory accessible by all threads in a process.
• Used in parallel computing for fast communication.
• Threads share global and heap data.
3. Why Use Shared Memory?
• Efficient data sharing compared to message passing.
• Low communication overhead.
• Commonly used with Pthreads and OpenMP.
4. Threads in C (Pthreads)
• Use pthread_create() to launch threads.
• Each thread runs a function in parallel.
• Threads share heap and global variables.
5. Shared Memory Model
• Each thread has its own stack.
• Heap and global sections are shared.
• Care needed to avoid race conditions.
6. Basic Pthread Program
• #include <pthread.h>
• pthread_create(), pthread_join() for managing threads.
• Code example shown in next slide.
7. C Code Snippet (Shared Memory Example)
• Example: Summing array elements using 4 threads.
• Each thread processes a segment of the array.
• Threads share the final result variable.
Code
#include <stdio.h>
#include <pthread.h>
#define THREAD_COUNT 4
#define ARRAY_SIZE 16
int array[ARRAY_SIZE]; // Shared array
long total_sum = 0; // Shared result variable
pthread_mutex_t lock; // Mutex for synchronization
void* sum_array(void* arg) {
int thread_id = *(int*)arg;
int start = thread_id * (ARRAY_SIZE / THREAD_COUNT);
int end = start + (ARRAY_SIZE / THREAD_COUNT);
8. long local_sum = 0;
for (int i = start; i < end; i++) {
local_sum += array[i];
}
// Update shared total_sum safely
pthread_mutex_lock(&lock);
total_sum += local_sum;
pthread_mutex_unlock(&lock);
return NULL;
}
9. int main() {
pthread_t threads[THREAD_COUNT];
int thread_ids[THREAD_COUNT];
pthread_mutex_init(&lock, NULL);
// Initialize array
for (int i = 0; i < ARRAY_SIZE; i++)
array[i] = i + 1; // Values 1 to 16
// Create threads
for (int i = 0; i < THREAD_COUNT; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, sum_array, &thread_ids[i]);
}
// Join threads
for (int i = 0; i < THREAD_COUNT; i++)
pthread_join(threads[i], NULL);
pthread_mutex_destroy(&lock);
printf("Total sum = %ldn", total_sum);
return 0;
}
10. Synchronization Need
• Shared variables require synchronization.
• Mutex locks prevent race conditions.
• Barriers ensure threads wait for each other.
11. Pthread Mutex
• pthread_mutex_t lock;
• pthread_mutex_lock() / pthread_mutex_unlock()
• Used to control access to shared resources.
13. Parallel Sum Example Diagram
• Array split into 4 chunks.
• Each thread processes a chunk in parallel.
• Results combined in a shared variable.
14. Performance Considerations
• Speedup = T_serial / T_parallel.
• More threads → less time (up to a point).
• Overhead of synchronization and context switching.
15. Runtime vs Threads (Graph)
• Graph: 1, 2, 4, 8 threads.
• Shows performance improvement.
• Plateau due to overhead.
17. Shared vs Message Passing
• Shared memory: low overhead, but risk of data races.
• Message passing (MPI): safer but slower.
• Choosing depends on application.
18. Summary of Key Points
• Shared memory allows fast inter-thread communication.
• Pthreads offer powerful thread control.
• Performance depends on synchronization and workload.