3
在更大的一段代码,我注意到g_atomic_ *在glib函数没有做什么我的预期,所以我写了这个简单的例子:GLIB:g_atomic_int_get变为NO-OP?
#include <stdlib.h>
#include "glib.h"
#include "pthread.h"
#include "stdio.h"
void *set_foo(void *ptr) {
g_atomic_int_set(((int*)ptr), 42);
return NULL;
}
int main(void) {
int foo = 0;
pthread_t other;
if (pthread_create(&other, NULL, set_foo, &foo)== 0) {
pthread_join(other, NULL);
printf("Got %d\n", g_atomic_int_get(&foo));
} else {
printf("Thread did not run\n");
exit(1);
}
}
当我编译这与海湾合作委员会的“-E”选项(预处理后停止),我注意到调用g_atomic_int_get(&富)已经成为:
(*(&foo))
和g_atomic_int_set(((INT *)PTR),42)已经成为:
((void) (*(((int*)ptr)) = (42)))
很明显,我期待一些原子比较和交换操作,而不仅仅是简单的(线程不安全)分配。我究竟做错了什么?
仅供参考我的编译命令如下:
gcc -m64 -E -o foo.E `pkg-config --cflags glib-2.0` -O0 -g foo.c
有趣的是你有没有需要内存屏障的任何文档(uname -a给出了“Linux rhel-5.4-dev 2.6.18-164.15.1.el5xen#1 SMP Mon Mar 1 11:11: 42 EST 2010 x86_64 x86_64 x86_64 GNU/Linux“在我的系统上)。我没有意识到有任何这样的架构是真实的,所以我想多做一些阅读。 – laslowh 2011-04-09 21:24:50