【发布时间】:2013-12-03 14:43:56
【问题描述】:
我正在开发一个需要在多线程环境中执行队列操作的程序。 我不确定函数的线程本地存储,而不仅仅是全局变量 我试过了
__thread int head,tail;
__thread int q[MAX_NODES+2];
__thread void enqueue (int x) {
q[tail] = x;
tail++;
color[x] = GRAY;
}
__thread int dequeue () {
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
我收到以下错误
fordp.c:71: error: function definition declared '__thread'
fordp.c:77: error: function definition declared '__thread'
我在某处读到一个函数已经是线程安全的,除非它使用共享变量,所以我尝试了
__thread int head,tail;
__thread int q[MAX_NODES+2];
void enqueue (int x) {
q[tail] = x;
tail++;
color[x] = GRAY;
}
int dequeue () {
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
它确实编译没有错误,但是我的执行结果是错误的提示队列在多线程平台上不能很好地工作。
谁能解释一下这里发生了什么?
感谢任何帮助。
【问题讨论】:
标签: c multithreading pthreads queue