Generated by GPT-5-mini| pthread | |
|---|---|
| Name | pthread |
| Title | POSIX Threads |
| Developer | IEEE, The Open Group, various vendors |
| Released | 1995 |
| Latest release | Specification revisions via IEEE Std 1003.1 |
| Programming language | C (programming language), C++ |
| Operating system | Unix, Linux, FreeBSD, macOS, Solaris |
| License | varies (standards + implementations) |
| Website | IEEE / The Open Group documentation |
pthread The POSIX threads API provides a standardized interface for creating, controlling, and synchronizing threads on POSIX-compliant systems. Originating from work by IEEE and formalized in POSIX.1c as part of IEEE Std 1003.1, the interface underpins multithreading support in many Unix-family operating systems and influences threading models in Linux, BSD, and macOS distributions. Implementations are provided by vendors such as GNU Project (via glibc), Sun Microsystems (historically for Solaris), and projects within FreeBSD.
The API defines types, functions, and semantics for managed threads, including lifecycle operations, synchronization, and attribute control. It complements other standards like C11 (C standard) threading features and integrates with system facilities such as signal handling in Linux and scheduling policies in POSIX.1-2008. The specification clarifies behavior for thread-safety of library functions, interaction with process-level notions from UNIX System V and Berkeley Software Distribution lineage, and constraints when combining with kernel-level threading and user-level threading frameworks.
Core types include pthread_t (thread identifier), pthread_attr_t (thread attributes), pthread_mutex_t (mutex), pthread_cond_t (condition variable), pthread_rwlock_t (read–write lock), and pthread_key_t (thread-specific data). Principal functions: pthread_create, pthread_join, pthread_detach, pthread_exit manage threads; pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_destroy control mutual exclusion; pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast handle condition synchronization; pthread_key_create, pthread_setspecific, pthread_getspecific implement thread-local storage. Attribute interfaces such as pthread_attr_init and pthread_attr_setdetachstate allow tuning for stack size, detach state, and scheduling parameters linked to SCHED_FIFO or SCHED_RR policies from POSIX.
Threads are spawned via pthread_create, supplying a start routine and argument; resources can be reclaimed via pthread_join or left detached with pthread_detach. Attributes permit control over stack size and scheduling inheritance, which interact with kernel facilities in Linux kernel, FreeBSD kernel, and macOS Mach ports. Cancellation points and cancellation types enable cooperative thread termination; pthread_cancel and pthread_setcancelstate manage cancellation behavior. Signal handling requires careful use of pthread_sigmask and sigwait in multithreaded programs, reflecting semantics defined in POSIX.1-2008 and modeled against traditional signal(7) behavior.
Mutexes offer basic exclusion, with attributes supporting recursive and error-checking behavior; robust mutexes survive certain failure modes, as specified for fault-tolerant servers common in Solaris and Linux deployments. Condition variables pair with mutexes to wait for predicates; timed waits use clock sources like CLOCK_REALTIME or CLOCK_MONOTONIC per POSIX clocks. Read–write locks optimize concurrency for reader-heavy workloads; barriers synchronize a group of threads. Spinlocks are available in some implementations for low-latency kernels such as Linux kernel and FreeBSD kernel where busy-wait behavior is tolerable.
Threads share process address space, so concurrent access to mutable data requires synchronization to avoid data races; this interacts with language-level memory models such as C11 (C standard) atomics and C++11 memory_order semantics. Stack limits and guard pages are controlled by thread attributes and influenced by system resource limits from ulimit semantics. Care is required when using asynchronous-signal-safe functions in handlers; only a listed subset of functions may be safely called from signal contexts, as documented in POSIX.1-2008. Deadlocks, priority inversion, and livelocks arise in multithreaded programs; techniques like priority inheritance and careful lock ordering—practiced in systems projects like Linux Kernel subsystems and Apache HTTP Server—mitigate risks.
Implementations include the GNU C Library's NPTL for Linux, the pthreads-win32 project providing a compatibility layer on Microsoft Windows, and native support in FreeBSD, OpenBSD, and macOS via their runtime libraries. Conformance depends on libc and kernel interaction; differences exist in thread scheduling granularity, stack handling, and low-level syscalls such as clone(2) on Linux or thr_create on Solaris. Standards revisions by The Open Group and the IEEE guide portability choices; many cross-platform projects such as Mozilla Firefox, Chromium, and PostgreSQL abstract threading via wrappers to accommodate platform-specific behavior.
Common patterns include worker-thread pools, producer–consumer queues, and thread-per-connection servers used in projects like Nginx, Apache HTTP Server, and Redis. Best practices: prefer portable synchronization primitives over ad-hoc spin loops; use pthread_attr_setstacksize to avoid near-default stack overflow on deep recursion; avoid calling non-async-signal-safe functions in signal handlers; document and enforce lock ordering to prevent deadlocks as in Linux Kernel coding guidelines. Testing with tools such as Valgrind, Helgrind, and sanitizer suites from LLVM helps detect races and UB. For cross-platform portability, wrap POSIX calls in an abstraction layer—employed by Boost (C++ libraries) and Qt—and follow the latest POSIX recommendations.
Category:Application programming interfaces