POSIX Threads
By IEEE (POSIX standard)
POSIX Threads, commonly called pthreads, is the standardized threading API defined as part of the POSIX specification for creating and managing multiple threads of execution within a single process on Unix-like operating systems. It…
Definition
POSIX Threads, commonly called pthreads, is the standardized threading API defined as part of the POSIX specification for creating and managing multiple threads of execution within a single process on Unix-like operating systems. It defines functions for thread creation, joining, and termination, along with synchronization primitives like mutexes and condition variables, giving C and C++ programs a portable, low-level way to write concurrent, shared-memory multithreaded code across different POSIX-compliant systems.
Overview
Pthreads was standardized to solve the same fragmentation problem POSIX addresses more broadly: before the standard, different Unix vendors had their own incompatible threading libraries, so multithreaded code written for one system's threading API would not port to another without significant rewriting. By defining a single, portable interface, IEEE gave developers a threading model, function signatures, and semantics that any POSIX-compliant operating system, including Linux, macOS, and the BSDs, would implement consistently, so multithreaded C code could move between platforms with far fewer changes. Mechanically, pthreads provides a set of C functions, most centrally pthread_create to spawn a new thread running a specified function, pthread_join to block until a thread finishes and retrieve its return value, and pthread_exit to terminate a thread. Because threads within a process share the same memory space, pthreads also defines synchronization primitives needed to coordinate safe access to shared data: mutexes to ensure only one thread accesses a critical section at a time, condition variables to let threads wait efficiently for a particular state to become true, and read-write locks for cases where many readers can proceed concurrently but writers need exclusive access. Getting this synchronization right is the hard part of pthreads programming, since a missed lock or incorrect ordering produces race conditions or deadlocks that can be difficult to reproduce and debug. Pthreads sits at a lower level of abstraction than OpenMP, which uses compiler directives to parallelize loops without requiring the programmer to manage individual threads and locks explicitly; pthreads gives full control over exactly when threads are created, what each one does, and how they synchronize, at the cost of significantly more code and more opportunities for subtle bugs. Higher-level language threading facilities, such as C++'s std::thread and std::mutex introduced in C++11, are often implemented on top of pthreads on POSIX systems, providing a more ergonomic, type-safe wrapper around the same underlying OS threading primitives. In practice, pthreads is used directly in performance-sensitive C and C++ systems software, language runtimes, and libraries that need precise control over thread behavior, and indirectly by nearly everything else through higher-level abstractions built on top of it. Database engines, web servers, and other software that needs fine-grained control over worker thread pools and synchronization commonly interact with pthreads directly or through a thin wrapper. The trade-offs are the ones inherent to low-level shared-memory threading generally: manual lock management is error-prone, incorrect synchronization produces race conditions and deadlocks that pass testing but fail intermittently in production, and pthreads code requires more careful reasoning than higher-level parallel programming models like OpenMP or language-level concurrency primitives, which trade some control for substantially reduced risk of these classes of bugs.
Specification
- Standardized thread creation, joining, and termination functions
- Mutexes for protecting shared data from concurrent access
- Condition variables for efficient thread wait/signal coordination
- Read-write locks allowing concurrent readers with exclusive writers
- Portable across all POSIX-compliant operating systems
- Foundation underlying higher-level threading APIs like C++ std::thread
- Fine-grained, explicit control over individual thread behavior
Use Cases
Alternatives
Frequently Asked Questions
From the Blog
Concurrency in Python: Threads, Processes, and asyncio
Choosing between threads, processes and asyncio in Python comes down to one question: is your work waiting on I/O or burning CPU. This guide makes that distinction precise, explains what the GIL actually blocks, and shows the failure modes — sequential awaits, blocking calls, unbounded fan-out — that make async code disappoint.
Read More ProgrammingHow to run blocking code inside an asyncio application
One blocking call stalls every coroutine sharing the event loop, so the fix is always to move it off. Learn to spot the offender through loop lag, choose threads for I/O-bound libraries and processes for CPU-bound work, size the executor honestly, and handle the cancellation boundary.
Read More