GLIB:g_atomic_int_get变成NO-OP?

在一段较大的代码中,我注意到glib中的g_atomic_ *函数没有达到我所期望的,所以我写了这个简单的例子:

#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); } } 

当我用GCC的'-E'选项(在预处理之后停止)编译这个时,我注意到对g_atomic_int_get(&foo)的调用变成:

 (*(&foo)) 

和g_atomic_int_set(((int *)ptr),42)已经变成:

 ((void) (*(((int*)ptr)) = (42))) 

显然,我期待着一些primefaces比较和交换操作,而不仅仅是简单(线程不安全)的分配。 我究竟做错了什么?

为了参考,我的编译命令如下所示:

 gcc -m64 -E -o foo.E `pkg-config --cflags glib-2.0` -O0 -g foo.c 

您所在的体系结构不需要原子整数集/获取操作的内存屏障,因此转换是有效的。

这是它的定义: http : //git.gnome.org/browse/glib/tree/glib/gatomic.h#n60

这是一件好事,否则你需要为每个原子操作锁定一个全局互斥锁 。