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 %dn", g_atomic_int_get(&foo));
} else {
printf("Thread did not runn");
exit(1);
}
}
当我使用GCC的'-E'选项编译它时(在预处理后停止),我注意到对g_atomic_int_get(&amp; foo)的调用已变为:
(*(&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
没有找到相关结果
已邀请:
1 个回复
蜗仓馈