Home

gettid

gettid is a Linux-specific system call that returns the thread ID (TID) of the calling thread. The TID is a kernel-assigned identifier that uniquely identifies a thread within the system, and it is distinct from the process ID (PID) and the thread group ID (TGID) used by the process as a whole.

In Linux, a process can consist of multiple threads that share the same TGID (and thus the

Calling gettid in C is done through a system call, commonly via syscall(SYS_gettid). For example, you can

#include <unistd.h>

#include <sys/syscall.h>

pid_t tid = syscall(SYS_gettid);

Some C libraries may provide a wrapper around this system call, but gettid is not part of

Portability and usage considerations: Because gettid is Linux-specific, code that relies on it is not portable

value
returned
by
getpid),
but
each
thread
has
its
own
TID.
The
main
thread’s
TID
is
typically
equal
to
the
process’s
PID,
while
other
threads
have
different
TIDs.
TIDs
are
unique
only
for
the
lifetime
of
the
thread
and
may
be
recycled
after
a
thread
terminates.
obtain
the
current
thread’s
TID
with:
the
POSIX
standard
and
is
not
portable
to
non-Linux
systems.
to
other
Unix-like
systems
(such
as
BSD
or
macOS)
without
alternative
mechanisms.
It
is
mainly
useful
for
kernel-level
debugging,
tracing,
or
when
interfacing
with
tools
and
interfaces
that
expect
a
kernel
thread
ID.
For
user-space
thread
identification
within
a
process,
pthread_self
or
thread-local
storage
often
suffices,
but
those
identifiers
do
not
necessarily
correlate
with
the
kernel’s
TID.
Reading
/proc/self/task
can
show
the
TIDs
of
all
threads
in
the
process.