Kru*_*lur 4 c++ portability gcc thread-local
由于每个编译器都有自己的线程本地存储版本,因此我最终为它创建了一个宏.现在唯一的问题是GCC(关闭了pthreads),它给了我:
"此目标不支持线程本地存储"
很公平,因为在这种情况下实际上关闭了pthreads.问题是,是否存在使用某些宏检测此问题的通用方法,例如#ifdef __GCC_XXX_NO_THREADS_XXX?
编辑:请参阅下面接受的答案.另外,这是我的懒惰解决方案:
$ touch test.c
$ gcc -E -dM test.c > out.1
$ gcc -pthread -E -dM test.c > out.2
$ diff out.*
28a29
> #define _REENTRANT 1
Run Code Online (Sandbox Code Playgroud)
这是在Mac OS X上.我不确定它是否可移植或任何东西......
你的编译命令行有-lpthread没有:你也可以包含一个-DHAVE_PTHREADS.
如果你真的想要GCC/ELF特定的运行时检测,你可以使用弱refs:
#include <pthread.h>
extern void *pthread_getspecific(pthread_key_t key) __attribute__ ((weak));
int
main()
{
if (pthread_getspecific)
printf("have pthreads\n");
else
printf("no pthreads\n");
}
Run Code Online (Sandbox Code Playgroud)
这是它的样子:
$ gcc -o x x.c
$ ./x
no pthreads
$ gcc -o x x.c -lpthread
$ ./x
have pthreads
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5432 次 |
| 最近记录: |