×

Search anything:

Multithreading and pthread in C

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 15 minutes

In this article, we have explored how the pthread library in C can be used to implement concepts of multithreading. A thread is a single sequence stream within in a process. As threads have some of the properties of processes, they are sometimes called lightweight processes. In short, thread is a unit of a process.

What are the differences between process and thread?

Threads are not independent of one other like processes as a result threads shares with other threads their:

  • code section
  • data section
  • OS resources like open files and signals

Like process, a thread has its own:

  • program counter (PC)
  • a register set
  • a stack space.

Multithreading

Thread is a programming technique to improve application performance through parallel processes. For example, in a browser, multiple tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs and so on.

Threads operate faster than processes due to following reasons:

  1. Thread creation is much faster
  2. Context switching between threads is much faster
  3. Threads can be terminated easily
  4. Communication between threads is faster

Multithreading is not supported by the language standard in C. POSIX Threads (or Pthreads) is a POSIX standard for threads. Implementation of pthread is available with gcc compiler.

Following is a basic program using pthreads:

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <pthread.h> 
  
void *myThread(void *vargp) 
{ 
    sleep(1); 
    printf("Inside Thread \n"); 
    return NULL; 
} 
   
int main() 
{ 
    pthread_t thread_id; 
    printf("Before Thread\n"); 
    pthread_create(&thread_id, NULL, myThread, NULL); 
    pthread_join(thread_id, NULL); 
    printf("After Thread\n"); 
    exit(0); 
}

Explanation of the above code

In main(), we declare a variable called thread_id, which is of type pthread_t, which is an integer used to identify the thread in the system. After declaring thread_id, we call pthread_create() function to create a thread. pthread_create() takes 4 arguments.

  • The first argument is a pointer to thread_id which is set by this function.
  • The second argument specifies attributes. If the value is NULL, then default attributes shall be used.
  • The third argument is name of function to be executed for the thread to be created.
  • The fourth argument is used to pass arguments to the function, myThread.

The pthread_join() function for threads is the equivalent of wait() for processes. A call to pthread_join blocks the calling thread until the thread with identifier equal to the first argument terminates.

Compile the above code using:


gcc code.c -lpthread
OpenGenus Tech Review Team

OpenGenus Tech Review Team

The official account of OpenGenus's Technical Review Team. This team review all technical articles and incorporates peer feedback. The team consist of experts in the leading domains of Computing.

Read More

Improved & Reviewed by:


Multithreading and pthread in C
Share this