linuxthreads 和 nptl 有具体定义吗
我有一个程序,对于 linuxthreads 和 nptl 来说,它的工作方式必须不同。
这个库中是否有定义,可以在我的程序中使用来检测,是使用 nptl 还是使用 linuxthreads?
UPDATE1:对于运行时,有一个 getconf GLIBC_LIBPTHREADS,但是对于编译时呢?
I hav a programme, which must work differently for linuxthreads and nptl.
Are there defines in this libs, that can be used in my programme to detect, is nptl is used or is linuxthreads is?
UPDATE1: For runtime there is a getconf GLIBC_LIBPTHREADS, but what for compile-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来这是不可能的,您可以在加载时更改实现,因此无论您做什么,都无法在编译时知道。
来自 pthreads 手册页:
更不用说这两种实现(大部分)是二进制兼容的,所以基本上你无法在编译时知道将使用哪个线程库,因为它可能会根据程序运行时存在的环境变量而改变运行,或者有人可以将二进制文件从 NPTL 系统复制到 LinuxThreads 系统。你就是做不到,因为它不是编译时已知的东西,至少不是你可以依赖的方式。
您必须找到某种方法来使用运行时检测,或者也许您可以使用有关为什么要执行此操作的信息来更新您的帖子,并且有人可能会提供有关如何以其他方式完成它或如何实现它的建议可以使用运行时检测正在使用哪些 pthread。
另一种可能的解决方案是向配置脚本添加一个选项,并让编译它的人进行选择。
Doesn't look like this is possible, you can change the implementation at load time so there's no way to know at compile time no matter what you do.
from the pthreads man page:
Not to mention that the two implementations are (mostly) binary compatible, so basically you CANNOT know at compile time which thread library will be used, EVER, because it might change depending on the environment variables present when your program is run, or someone could copy the binary from an NPTL system to a LinuxThreads system. You just can't do it, because it's not something that is known at compile time, at least not in a way you can rely on.
You'll have to find some way to use run time detection, or maybe you could update your post with information about WHY you want to do this and someone could maybe offer advice about how to accomplish it some other way, or how to make it possible to use run time detection of which pthreads is in use.
The other possible solution is to add an option to your configure script and make the person compiling it choose.
让我们退一步问——你为什么要这样做?
是否存在一种功能是一种功能而另一种功能则没有的?如果是这样,也许你可以在
libpthread 上使用
在运行时动态查询它们的存在。dlsym
.so或者更多的是它们之间的某些行为不同,导致您的程序行为不当?如果是这样,我会避免依赖此类行为的结果,或者参考 POSIX 等标准来确定您可以依赖什么。通常,此类可移植性错误代表了代码中的真正缺陷,即使使用您认为正在“工作”的库,也可能需要解决这些缺陷。当并发出现时,正确处理这一点尤为重要。
最后,如果是结构大小的问题......也可能有一些奇怪的方法来解决这个问题。例如(只是一个例子,可能完全偏离基础,但说明了这个想法):
非常hacky和非常丑陋,但只是将这些想法作为可能的解决方法扔在那里......
Let's take a step back and ask - why do you want to do this?
Are there functions that one provides that another does not? If so perhaps you can use
dlsym
onlibpthread.so
to dynamically query their existence at runtime.Or is it more a matter of some behavior that differs between them, causing your program to misbehave? If so, I would avoid relying on the outcome of such behaviors, or consult a standard like POSIX to determine what you can rely on. Often such portability bugs represent real flaws in your code that might need addressing even using the library you think is "working". When concurrency enters the picture this is especially important to get right.
Lastly, if it's a matter of the sizes of structures... There may be hacky ways around this too. For example (just an example, may be totally off-base, but illustrates the idea):
Very hacky and very ugly, but just throwing these kinds of ideas out there as a possible workaround...
查看标题后,没有 - NPTL 与 LinuxThreads 没有具体的定义。
如果您需要这样的定义,请编写一个生成头文件的小脚本,或将定义标志传递给编译器。您可以通过解析 /lib/libc.so.6 的输出来获取信息(该库可以作为普通可执行文件直接运行)。我将把细节留给你,但输出看起来像:
LinuxThreads:
NPTL:
(不过,尝试编写不需要处理这个问题的代码。到目前为止,我们还没有遇到任何需要这样做的情况)我们在 RHEL 4(linuxthreads)与 RHEL 5(NPTL)上运行的多线程应用程序
After looking through the headers, no - there are no specific defines for NPTL vs LinuxThreads.
If you need such a define, write a little script that produces a header file, or pass a define flag to the compiler. You can get the info by parsing the output of /lib/libc.so.6 (that library can be run direcly as a normal executable). I'll leave the details to you, but the output looks like:
LinuxThreads:
NPTL:
(though, try to rather write code that doesn't need to deal with this. So far, we've not encountered any need for this in our multithreaded apps that run on RHEL 4(linuxthreads) vs RHEL 5(NPTL))