我已经声明了一个变量:
静态 __thread int a;
我收到以下错误:
fatal error (dcc:1796): __thread 在指定的目标环境中不受支持
我该如何解决这个问题?我应该在 make 文件中启用一些标志吗?
我正在使用 windriver 编译器(为 powerpc 编译)。我提到了类似的问题,但无法弄清楚。
基本上我正在尝试制作可重入函数。任何建议都会有很大帮助。
我可以通过包含 pthread.h 做些什么吗?
谢谢。
最佳答案
__thread 是 gcc 扩展,不能在所有平台上工作。如上所述,您可以使用 pthread_setspecific/pthread_getspecific,有一个来自 man 的示例:
/* Key for the thread-specific buffer */
static pthread_key_t buffer_key;
/* Once-only initialisation of the key */
static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
/* Allocate the thread-specific buffer */
void buffer_alloc(void)
{
pthread_once(&buffer_key_once, buffer_key_alloc);
pthread_setspecific(buffer_key, malloc(100));
}
/* Return the thread-specific buffer */
char * get_buffer(void)
{
return (char *) pthread_getspecific(buffer_key);
}
/* Allocate the key */
static void buffer_key_alloc()
{
pthread_key_create(&buffer_key, buffer_destroy);
}
/* Free the thread-specific buffer */
static void buffer_destroy(void * buf)
{
free(buf);
}
但正如我所见,您正在尝试制作可重入函数,可重入函数不应保存静态非常量数据。
关于c - 线程本地存储 (TLS) - 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6043838/