set_tid_address(2)
π set_tid_address(2) β the syscall manual page, bestie edition π
okay so this is a page from the System Calls Manual, and we're talking about set_tid_address. it's giving "tiny syscall, big responsibilities", and honestly? iconic. let's get into it π«Ά
β¨ NAME β¨
set_tid_address β set pointer to thread ID
π LIBRARY π
Standard C library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
pid_t syscall(SYS_set_tid_address, int *tidptr);
π¨ bestie, important heads up: glibc provides no wrapper forset_tid_address(), so you literally have to go throughsyscall(2). no shortcut, no cute little libc function, you're doing it raw. periodt.
π DESCRIPTION π
so for every single thread, the kernel keeps two attributes (they're addresses, js) called set_child_tid and clear_child_tid. and out of the box, both of these are NULL. like, default state: nothing, nobody, empty. π«οΈ
π§Έ set_child_tid
if a thread gets started with clone(2) and the CLONE_CHILD_SETTID flag, then set_child_tid gets set to the value that was passed in the ctid argument of that syscall.
and when set_child_tid is set? the very first thing the new thread does β like, before anything else, main character behavior β is write its thread ID at this address. no cap. π«
π§Ή clear_child_tid
if a thread gets started with clone(2) and the CLONE_CHILD_CLEARTID flag, then clear_child_tid gets set to the value passed in the ctid argument of that syscall.
and the syscall set_tid_address() β that's our girl β sets the clear_child_tid value for the calling thread to tidptr.
now here's the juicy part π: when a thread whose clear_child_tid is not NULL terminates, and if that thread is sharing memory with other threads, then 0 gets written at the address specified in clear_child_tid, and the kernel runs this:
futex(clear_child_tid, FUTEX_WAKE, 1, NULL, NULL, 0);
what does that do? it wakes up a single thread that's doing a futex wait on that memory location. one thread. one. and any errors from the futex wake operation? ignored. not our problem, not our vibe. π
π RETURN VALUE π
set_tid_address() always returns the caller's thread ID. every time. no plot twists.
π ERRORS π
set_tid_address() always succeeds. literally cannot fail. the reliability is unmatched, fr fr. π
π STANDARDS π
Linux.
π¦ HISTORY π¦
Linux 2.5.48.
and the details as given here? those have been valid since Linux 2.5.49.
π SEE ALSO π
and that's the whole page, bestie!! you ate that. go forth and set your tid pointers responsibly π
sign-off time: this lovely little manual page comes to you from Linux man-pages 6.20, dated 2026-02-08. mwah π β¨ππ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2004, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms