views:

38

answers:

2

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.

A: 

Find out which <sys/types.h> could be included in your C translation units (by checking along oyur includes path(s)). pid_t is defined there. It's a signed integral type, but there are a few of those. It could easily be wider than a long.

The Open Groups documentation of sys/types.h promises "The implementation shall support one or more programming environments in which the widths of blksize_t, pid_t, size_t, ssize_t, suseconds_t, and useconds_t are no greater than the width of type long. The names of these programming environments can be obtained using the confstr() function or the getconf utility." So you can probably cast a pid_t to long (or at least use getconf to find out what you have to do to be in a situation where pid_t can be safely cast to long).

See C Language Gotchas: printf format strings for a discussion of why what you want to do is complicated, cannot be written portably, and may suddenly break in the future.

Eric Towers