Parallel Programming in C

Last Updated : 30 May, 2026

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.

  • Divides a large task into smaller subtasks that can run concurrently.
  • Improves resource utilization and speeds up program execution.

Parallelism in C

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:

  • Concurrency: It is defined as managing multiple tasks that start, run, and complete in overlapping time periods.
  • Parallelism: Running multiple tasks at the same time within the system to improve the overall performance.
  • Process: The program under execution is known as a process. Every program within the system is independent of each other and is executed in isolation within its own memory space.
  • Threads: Also known as lightweight processes, threads are defined as the smallest unit of processing that can be executed independently.

There are two methods through which parallel programming can be implemented in C:

  • Using POSIX (Pthreads)
  • Using OpenMP Library

Parallel Programming with POSIX

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:

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,

  • pthread_t: It is used to declare thread Id variables.
  • pthread_create: This method is used to create a thread
  • pthread_join: This method is used to join two threads so that they can wait until each of them completes their execution.
  • pthread_exit: This method is used to terminate a thread.

Example: Program to Implement Parallel Programming Using POSIX

C
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

// Define the number of threads to be created
#define NUM_THREADS 4

// Function to be executed by each thread
void* perform_work(void* arg)
{
    int index = *((int*)arg);
    printf("Thread %d: starting work...\n", index);
    // Perform some work here
    printf("Thread %d: work done.\n", index);
    // Return NULL to indicate the thread has finished
    // execution
    return NULL;
}

int main()
{
    // Array to hold thread identifiers
    pthread_t threads[NUM_THREADS];
    // Array to hold arguments for each thread
    int thread_args[NUM_THREADS];
    // Variable to hold return code from pthread functions
    int rc;

    for (int i = 0; i < NUM_THREADS; ++i) {
        // Set the thread argument to the current index
        thread_args[i] = i;
        // Create a new thread
        rc = pthread_create(&threads[i], NULL, perform_work,
                            (void*)&thread_args[i]);
        // Check if thread creation was successful
        if (rc) {
            // Print error message if thread creation failed
            printf("Error: unable to create thread, %d\n",
                   rc);
            exit(-1);
        }
    }

    // Loop to join the  threads
    for (int i = 0; i < NUM_THREADS; ++i) {
        // Wait for each thread to complete its execution
        pthread_join(threads[i], NULL);
    }

    return 0;
}


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().
  • The above example creates 4 worker threads, executes tasks concurrently using pthreads, and waits for all threads to finish before terminating the program.

Parallel Programming with OpenMP Library

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.

Syntax

#pragma omp parallel
{
// Code inside this block will execute in parallel
}

Example: Program to Implement Parallel Programming Using OpenMP Library

C
#include <omp.h>
#include <stdio.h>
int main()
{
    // Define the number of threads to be used
    int num_threads = 4;

    // Set the number of threads to use in parallel regions
    omp_set_num_threads(num_threads);

// Parallel region begins
#pragma omp parallel
    {
        // Get the ID of the current thread
        int thread_id = omp_get_thread_num();
        printf("Hello from thread %d\n", thread_id);
    }

    return 0;
}


Output

Hello from thread 2
Hello from thread 0
Hello from thread 3
Hello from thread 1

Compile and Run OpenMP Program

gcc program.c -o program -fopenmp

./program

Explanation:

  • -fopenmp enables OpenMP support in GCC and allows the compiler to process directives such as #pragma omp parallel.
  • The above example creates a parallel region with 4 threads, where each thread prints its own thread ID to demonstrate parallel execution using OpenMP.
Comment