我正在开发一个无锁数据结构,我正在尝试CAS指针。
使用compare和swap时,我应该将哪些标志传递给编译器? 我已获得http://locklessinc.com/内存分配器的许可证。 我也在使用netbeans 7。
目前我所拥有的唯一标志位于链接器下,-march = native -lllalloc。
然而,当我运行该程序时,我会遇到奇怪的内存问题。 我想我可能会错过一两个国旗...... netbeans也说“无法解析标识符__sync_bool_compare_and_swap”但它仍然编译它。
有人有什么想法吗?
数据结构:
struct Ambigous
{
short type;
union{
struct{
bool inCleanup;
KEY key;
VALUE value;
};
Ambigous* volatile list[MAIN_SIZE];//CHANGED ordering, still have netbeans error
};
};
我的比较和交换代码
bool res= __sync_bool_compare_and_swap(&(local->list[pos]), current_node, new_node);
我如何分配内存:
Ambigous *temp_spine = (Ambigous *) calloc(1,sizeof (Ambigous));
答案 0 :(得分:2)
__sync_bool_compare_and_swap
是GCC内在的。您不需要任何特定的编译器标志;但是,您的IDE将无法找到其定义,因为它没有。它可能不适用于其他编译器;例如,Visual Studio编译器将其称为InterlockedCompareExchange
至于你的问题所在,如果没有看到你的其余代码,很难说。无锁算法非常很难搞定;很容易错过一些小的竞争条件。坚持锁定,除非你有充分的理由不这样做。