Search code examples
cmultithreadingbsd

how to get thread ID as integer on BSD in C/C++?


Does anyone know to get the current thread ID as an integer on BSD?

i found this

#ifdef RTHREADS
  299     STD     { pid_t sys_getthrid(void); }
  300     STD     { int sys_thrsleep(void *ident, int timeout, void *lock); }
  301     STD     { int sys_thrwakeup(void *ident, int n); }
  302     STD     { int sys_threxit(int rval); }
  303     STD     { int sys_thrsigdivert(sigset_t sigmask); }
#else
  299     UNIMPL
  300     UNIMPL
  301     UNIMPL
  302     UNIMPL
  303     UNIMPL
#endif

and tried (long)syscall(229) but does not work (it crashes). On Linux i can get thread ID with system call (long) syscall(224) which gives me an integer (usually 4 digits). Anyone can help?! Thank you.


Solution

  • There is no such thing as "BSD". Every *BSD system is completely different, especially when it comes to threads. Even within single project like FreeBSD there are various pthread implementations (libc_r, kse, thr) that vary between os versions and user configuration.

    Having said that, on FreeBSD-8 there should be int thr_self(long *id) in /usr/include/sys/thr.h and on reasonably fresh NetBSD there is lwpid_t _lwp_self(void) in /usr/include/lwp.h.

    For more platforms you can take a look at int get_unix_tid(void) in wine source.