![]() |
VOOZH | about |
Parallel Programming is a computing technique that allows multiple tasks or computations to be executed simultaneously, improving performance and reducing execution time. It is widely used in multi-core processors and distributed systems to handle large and complex workloads efficiently.
Before you learn to implement parallel programming in C make sure you are familiar with the following key concepts related to parallel programming in C:
There are two methods through which parallel programming can be implemented in C:
POSIX threads also known as pthreads is a standard interface used for creating threads in C. It provides a set of API's through which we can easily manage the threads and achieve parallel programming. To use POSIX threads in your program include the <pthreads.h> header file in your program and follow the below syntax:
pthread_t thread_id;
// Create thread
pthread_create(&thread_id, NULL, thread_function, NULL);// Wait for threads to complete
pthread_join(thread_id, NULL);// Terminate Thread
pthread_exit(NULL);
Here,
Example: Program to Implement Parallel Programming Using POSIX
Output
Thread 1: starting work...
Thread 1: work done.
Thread 0: starting work...
Thread 0: work done.
Thread 2: starting work...
Thread 2: work done.
Thread 3: starting work...
Thread 3: work done.Compile and Run POSIX Program
gcc program.c -o program -lpthread
./program
Explanation:
-lpthread links the POSIX thread library required for pthread functions such as pthread_create() and pthread_join().OpenMp short for Open Multi-Processing is a popular library that provides a set of API's that supports multi-platform shared memory processing in C and C++. It used the compiler directives to achieve parallelism in the programs. To use the openMp library include the <omp.h> header file in your code and follow the below syntax.
#pragma omp parallel
{
// Code inside this block will execute in parallel
}
Example: Program to Implement Parallel Programming Using OpenMP Library
Output
Hello from thread 2
Hello from thread 0
Hello from thread 3
Hello from thread 1Compile and Run OpenMP Program
gcc program.c -o program -fopenmp
./program
Explanation: