c++ - 是否有 linuxthreads 和 nptel 的特定定义

标签 c++ c nptl predefined-macro linuxthreads

我有一个程序,它对 linuxthreads 和 nptl 的工作方式必须不同。

这个库中是否有定义,可以在我的程序中用来检测,是使用了 nptl 还是使用了 linuxthreads?

更新 1:对于运行时,有一个 getconf GLIBC_LIBPTHREADS,但对于编译时呢?

最佳答案

这看起来不可能,您可以在加载时更改实现,因此无论您做什么,都无法在编译时知道。

来自 pthreads 手册页:

On systems with a glibc that supports both LinuxThreads and NPTL (i.e., glibc 2.3.x), the LD_ASSUME_KERNEL environment variable can be used to override the dynamic linker's default choice of threading implementation. This variable tells the dynamic linker to assume that it is running on top of a particular kernel version. By specifying a kernel version that does not provide the support required by NPTL, we can force the use of LinuxThreads. (The most likely reason for doing this is to run a (broken) application that depends on some nonconformant behavior in LinuxThreads.) For example:

bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \
                awk '{print $3}' ) | egrep -i 'threads|ntpl'
linuxthreads-0.10 by Xavier Leroy

更不用说这两个实现(大部分)是二进制兼容的,所以基本上你永远无法在编译时知道将使用哪个线程库,因为它可能会根据程序运行时存在的环境变量而改变,或者有人可以将二进制文件从 NPTL 系统复制到 LinuxThreads 系统。你就是做不到,因为它不是编译时已知的东西,至少不是你可以依赖的方式。

你必须找到一些方法来使用运行时检测,或者你可以更新你的帖子,说明你为什么要这样做,有人可能会提供关于如何以其他方式完成它的建议,或者如何使运行时检测正在使用的 pthreads 成为可能。

另一种可能的解决方案是向您的配置脚本添加一个选项,并让编译它的人选择。

关于c++ - 是否有 linuxthreads 和 nptel 的特定定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3512396/

相关文章:

c++ - 如何dlopen jemalloc动态库

c++ - Visual Studio 命令编译器强制 64 位编译

c - c中整数除法运算的方向

c,如何在 if 语句中使用 char 变量?

linux - NPTL 默认堆栈大小问题

c++ - 最佳实践 : Where should function comments go in C/C++ code?

c++ - 使用 Qt 5.2 静态库编译时在 Windows 上加载 Qt 应用程序时出错

c - 为什么不属于实现的名称仍然使用双下划线命名约定?

Java I/O 与带有 Linux NPTL 的 Java 新 I/O (NIO)

linux - 设置线程/proc/PID/cmdline?