开发者

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
开发者_如何学C

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜