Home

perthread

Perthread, or per-thread storage, is data that is allocated separately for each thread in a multi-threaded program. Each thread has its own copy, which is invisible to other threads unless explicitly accessed through thread-local handles. Perthread data persists for the lifetime of the thread and is automatically isolated from other threads.

Common implementations rely on thread-local storage (TLS). In C++, the thread_local keyword declares a variable with

Use cases include per-thread caches, per-thread logging buffers, per-thread errno-like state, and avoiding synchronization when shared

Advantages include reduced contention and avoidance of locks for thread-specific data; however, per-thread data increases overall

Related concepts include thread-local storage, thread-specific data, and the various language and OS APIs for TLS.

thread
storage
duration.
In
C,
compilers
typically
provide
__thread
or
thread_local.
Java
uses
ThreadLocal
to
hold
per-thread
values.
Python
provides
threading.local
to
create
per-thread
namespaces.
Operating
systems
offer
APIs
like
pthread_key_t
and
TlsAlloc
on
Windows
to
allocate
thread-specific
keys
and
store
data
via
pthread_setspecific
or
TlsSetValue.
data
is
mostly
immutable
or
read-mostly.
These
patterns
help
reduce
contention
by
keeping
thread-specific
state
out
of
shared
data
paths.
memory
usage
and
can
complicate
cleanup,
particularly
on
thread
termination
or
in
thread
pools.
Portability
considerations
arise
where
TLS
semantics
differ
or
where
threads
are
pooled
rather
than
created/destroyed
per
task.