Welcome to this comprehensive guide on C Barrier! š
In this lesson, we'll learn about the C Barrier, a synchronization primitive that helps manage concurrent processes, particularly useful when dealing with multiple threads in C. Let's dive in!
A Barrier in C is a synchronization construct that ensures a predefined number of threads reach a common point before any of them can proceed further. This mechanism is particularly useful when multiple threads work together to accomplish a task but need to combine their results or resources after completing their individual tasks.
Using a Barrier in C is essential for managing concurrent tasks efficiently, ensuring that threads work in a coordinated manner and preventing race conditions. It allows us to maintain program consistency and predictability, making our code more robust and reliable.
The pthread_barrier_t data type represents a barrier in C. To create a barrier, you first need to allocate memory for it using the pthread_barrier_init() function.
#include <pthread.h>
pthread_barrier_t barrier;
int num_threads; // Number of threads participating in the barrier
int main() {
// Initialize the barrier with the specified number of threads
pthread_barrier_init(&barrier, NULL, num_threads);
// Start your threads here...
// ... and join them at the end
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// Destroy the barrier when no longer needed
pthread_barrier_destroy(&barrier);
}š Note: Make sure to include the required header files and properly initialize and destroy the barrier to avoid memory leaks.
Each thread waits at the barrier using the pthread_barrier_wait() function. This function blocks the calling thread until all other threads associated with the barrier have reached the barrier as well.
#include <pthread.h>
void *worker(void *arg) {
// Perform some work here...
// Wait at the barrier
pthread_barrier_wait(&barrier);
// Proceed with more work or terminate the thread...
}In this example, we'll use a barrier to compute the sum of an array in parallel.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define SIZE 100000
#define THREADS 4
pthread_barrier_t barrier;
long long total = 0;
long long local_sum[THREADS];
long long arr[SIZE];
void *sum_part(void *arg) {
long long *start = (long long *) arg;
long long end = (long long *) (arg + 1);
for (long long i = *start; i < end; ++i)
total += arr[i];
// Wait at the barrier
pthread_barrier_wait(&barrier);
// Add the partial sum to the global total
for (int i = 0; i < THREADS; ++i)
total += local_sum[i];
}
int main() {
// Initialize the barrier with the specified number of threads
pthread_barrier_init(&barrier, NULL, THREADS);
// Fill the array with some data...
// Create and start threads to compute partial sums
pthread_t threads[THREADS];
for (int i = 0; i < THREADS; ++i) {
long long *args[2] = {&arr[i * (SIZE / THREADS)], &arr[(i + 1) * (SIZE / THREADS) - 1]};
pthread_create(&threads[i], NULL, sum_part, args);
}
// Wait for all threads to finish
for (int i = 0; i < THREADS; ++i)
pthread_join(threads[i], NULL);
// Destroy the barrier when no longer needed
pthread_barrier_destroy(&barrier);
printf("The sum is: %lld\n", total);
return 0;
}This example demonstrates the efficient computation of a large sum by splitting the array into smaller parts and distributing those parts among multiple threads. The barrier ensures that all partial sums are combined before computing the final result, preventing race conditions and ensuring a correct solution.
Which function is used to block a thread at a barrier in C?
By the end of this lesson, you should have a solid understanding of C Barriers and their use in synchronizing concurrent processes in C. Happy coding! š