Home

Pthreads

Pthreads, or POSIX threads, is a standardized API for writing multi-threaded programs in C, C++, and other languages on POSIX-compliant systems. Defined by POSIX.1c (IEEE Std 1003.1c), it specifies interfaces for creating and managing threads, synchronizing their execution, and handling thread-local data.

Core facilities include thread lifecycle: pthread_create, pthread_exit, pthread_join, pthread_cancel, and attributes via pthread_attr_t controlling detach state,

Additional features include thread-local storage, cancellation handling (pthread_setcancelstate, pthread_cancel, and cleanup handlers via pthread_cleanup_push/pthread_cleanup_pop), and scheduling

Portability and usage: pthreads is supported on most UNIX-like systems, including Linux, BSD variants, and macOS,

stack
size,
scheduling,
and
guard
size.
Synchronization
primitives:
mutexes
(pthread_mutex_t)
with
lock/unlock;
condition
variables
(pthread_cond_t)
for
signaling;
reader-writer
locks
(pthread_rwlock_t);
barriers
(pthread_barrier_t)
for
group
synchronization;
and
thread-specific
data
keys
(pthread_key_create,
pthread_getspecific,
pthread_setspecific).
control
through
pthread_setschedparam
and
scheduling
policies
(SCHED_FIFO,
SCHED_RR,
SCHED_OTHER).
The
API
also
provides
thread
attributes
for
detach
state
(joinable
or
detached)
and
various
stack-related
parameters,
enabling
fine-grained
control
over
thread
behavior.
where
the
library
is
typically
provided
as
part
of
the
system
C
library.
Windows
environments
achieve
POSIX
threading
behavior
through
compatibility
libraries
such
as
pthreads-w32
or
in
POSIX-compatibility
layers
like
Cygwin.
While
the
standard
promotes
portability,
exact
semantics
and
performance
can
vary
between
implementations.
Pthreads
remains
a
foundational
API
for
building
scalable
servers,
parallel
compute
tasks,
and
responsive
applications
on
POSIX
platforms.