开发者

__sync_bool_compare_and_swap Compiler Flags and Includes

开发者 https://www.devze.com 2023-03-06 13:36 出处:网络
I am working on a lock free data structure, and am trying to CAS pointers. When using compare and swap, what flags should I pass into the compiler?

I am working on a lock free data structure, and am trying to CAS pointers.

When using compare and swap, what flags should I pass into the compiler? I have gotten a license for http://locklessinc.com/ memory allocator. I am also using netbeans 7.

Currently The only flags I have are under the linker, -march=native -lllalloc.

However when I run the program I get weird memory issues. I think I may be missing a flag or two... Also netbeans says "unable to resolve identifier __sync_bool_compare_and开发者_StackOverflow中文版_swap" but it still compiles it.

Anyone have any ideas?

Data Structure:

struct Ambigous
{
    short type;
    union{
        struct{
            bool inCleanup;
            KEY key;
            VALUE value;
        };
        Ambigous* volatile list[MAIN_SIZE];//CHANGED ordering, still have netbeans error
    };

};

My Compare and swap code

bool res= __sync_bool_compare_and_swap(&(local->list[pos]), current_node, new_node);

How I allocate memory:

Ambigous *temp_spine = (Ambigous *) calloc(1,sizeof (Ambigous));


__sync_bool_compare_and_swap is a GCC intrinsic. You don't need any particular compiler flags for it; however, your IDE won't be able to find its definition, because it doesn't have one. And it may not work on other compilers; eg, the Visual Studio compilers call it InterlockedCompareExchange

As for where your problem is, it's hard to say without seeing the rest of your code. Lockless algorithms are very tricky to get right; it's all too easy to miss some small race condition. Stick to locks unless you have a very good reason to do otherwise.

0

精彩评论

暂无评论...
验证码 换一张
取 消