THR_SELF(2)
π
THR_SELF(2) β the thr_self era
okay so bestie, welcome to thr_self from section 2 of the manual β the System Calls Manual, to be exact. yes we're in the syscall neighborhood, not the polite libc one. pull up a chair π
β¨ NAME β¨
thr_self β returns the thread identifier for the calling thread. literally it's just "who am i, thread edition" and honestly? iconic self-discovery moment π
π LIBRARY π
Standard C Library (libc, -lc)
π«Ά SYNOPSIS π«Ά
#include <sys/thr.h>
int
thr_self(long *id);
πΈ DESCRIPTION πΈ
so the thr_self() system call stores the system-wide thread identifier for the current kernel-scheduled thread into the variable that the argument id points at. that's it. that's the whole job. and she ate π
the thread identifier is an integer in the range from PID_MAX + 2 (100001) all the way up to INT_MAX. no cap, those are the real bounds.
it's guaranteed to be unique at any given time, for each running thread in the system β like every thread gets its own little name tag π·οΈβ¨. but β and this is the part where you go "oh π" β after the thread exits, that identifier may be reused. so don't go treating it like a forever-unique snowflake ID, bestie. reuse is real π
π₯ RETURN VALUES π₯
- if it worked:
thr_self()returns zero π slay - if it didn't:
-1comes back, anderrnois set to indicate the error π
π ERRORS π
here's the whole error roster, and it's short so you can absolutely memorize it before chem class:
[EFAULT]β the memory that theidargument points to is not valid. so like... it's a bad pointer. she's pointing at nothing. ghost memory π»
π SEE ALSO π
_umtx_op(2), thr_exit(2), thr_kill(2), thr_kill2(2), thr_new(2), thr_set_name(2), pthread_getthreadid_np(3), pthread_self(3)
(go read these next, the whole thread friend group is lowkey fascinating π¦)
π STANDARDS π
real talk: the thr_self() system call is non-standard. it's not some blessed POSIX-certified icon, it's a FreeBSD special π
. it exists because the 1:1 Threading Library (libthr, -lthr) uses it to implement IEEE Std 1003.1-2001 (βPOSIX.1β) pthread(3) functionality β i.e. she's the backstage crew making the main-character pthreads look effortless β¨
π§ HISTORY π§
the thr_self() system call first appeared in FreeBSD 5.2. throwback era fr fr π
warning bestie:[EFAULT]is the only error listed, so if youridpointer is garbage, that's what you're getting. keep that pointer valid and hydrated π
that's the whole page, you understood it perfectly, i'm obsessed with you π«Άπ
β from the Debian edition of this page, dated May 5, 2020, sourced straight from the System Calls Manual, mwah πβ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2016 The FreeBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms