VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/posix-threads-in-os/

⇱ POSIX Threads in OS - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

POSIX Threads in OS

Last Updated : 15 Apr, 2026

POSIX thread libraries (pthreads) are a standard C/C++ API used to create and manage threads for concurrent execution within a program. They allow multiple flows of execution to run in parallel, especially improving performance on multi-core or multiprocessor systems. Since threads share the same memory space and resources, they have lower overhead compared to creating separate processes (like using fork). Even on single-processor systems, threads help improve efficiency by utilizing waiting time during I/O operations.

To use PThreads, we must include the header at the start of the C/C++ program.

#include <pthread.h>
  • PThreads (POSIX threads) is a standardized API for multithreading in C/C++ on Unix-like systems. It is defined by the POSIX standard, which specifies how operating systems should implement thread-related functionalities.
  • PThreads provides a set of functions (APIs) for creating, managing, and synchronizing threads within a program.

Uses

  • Improves program performance through multithreading
  • Enables concurrent execution of independent tasks
  • Achieves parallelism on multiprocessor/multi-core systems
  • Reduces overhead compared to process creation and management
  • Provides efficient communication via shared memory

Let's have a look at a C example of a better implementation approach

This program creates two threads, each executing a worker function that prints its unique identifier to the standard output. If interaction between threads is required, a global variable defined outside all functions can be used for shared access. The program can be compiled using the GCC compiler with the appropriate Pthreads library.

gcc pthreads_demo.c -lpthread -o pthreads_demo
  • gcc: GNU C Compiler used to compile the program
  • pthreads_demo.c: Source code file
  • -lpthread: Links the Pthreads library (required for thread functions)
  • -o pthreads_demo: Specifies the output executable file name

Windows Support for Pthreads

Pthreads are not natively supported on Windows. The Pthreads-w32 project provides a portable, open-source implementation that allows Unix-based applications using Pthreads to run on Windows with minimal changes. It supports 64-bit Windows systems with some additional setup.

Winpthreads, part of the MinGW-w64 project, is another implementation that uses more native Windows system calls, offering better integration and performance compared to Pthreads-w32.

Comment
Article Tags:
Article Tags: