如何在 C/C++ 中的 BSD 上获取整数形式的线程 ID?

发布于 2024-09-28 17:20:36 字数 604 浏览 26 评论 0原文

有谁知道在 BSD 上以整数形式获取当前线程 ID 吗?

我发现了这个

#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

并尝试了(长)syscall(229),但不起作用(它崩溃了)。在 Linux 上,我可以通过系统调用(长)syscall(224) 获取线程 ID,它给我一个整数(通常是 4 位数字)。任何人都可以帮忙吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-10-05 17:20:36

不存在“BSD”这样的东西。每个 *BSD 系统都完全不同,尤其是在线程方面。即使在 FreeBSD 这样的单个项目中,也存在各种 pthread 实现(libc_r、kse、thr),这些实现因操作系统版本和用户配置而异。

话虽如此,在 FreeBSD-8 上, /usr/include/sys/thr.h 并且在相当新的 NetBSD 上有 lwpid_t _lwp_self(void) 在 /usr/include/lwp.h

对于更多平台,您可以查看 int get_unix_tid(void)

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.

小女人ら 2024-10-05 17:20:36

找出哪个可以包含在您的 C 翻译单元中(通过检查您的包含路径)。 pid_t 在那里定义。它是一个有符号整数类型,但有一些这样的类型。它很容易比长的更宽。

sys/types.h 承诺的 Open Groups 文档 “该实现应支持一种或多种编程环境,其中 blksize_t、pid_t、size_t、ssize_t、suseconds_t 和 useconds_t 的宽度不大于 long 类型的宽度。这些编程环境的名称可以使用 confstr( ) 函数或 getconf 实用程序。”因此,您可能可以将 pid_t 强制转换为 long (或者至少使用 getconf 来找出必须做什么才能将 pid_t 安全地强制转换为 long)。

请参阅 C 语言陷阱:printf 格式字符串 进行讨论为什么你想做的事情很复杂,无法移植,并且将来可能会突然崩溃。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文