1
4

3 回答 3

4

You should pass the value of full2 and full3, not a pointer to it. Also, you should be care about the alignment.

__sync_bool_compare_and_swap((long long*)full,*(long long*)full2,*(long long*)full3);

(Of course, this is not portable. If you want portability, use uint64_t instead of long long)

于 2012-02-13T00:30:30.657 回答
3

Looks like you forgot to deref your pointers and cast.

I tested and this is the only combination that is correct:

__sync_bool_compare_and_swap((long long*)full, *(long long *)full2, *(long long *)full3);

You need to cast the first param or it will only swap the first char.

Regarding handling 128-bit double long, this is from the gcc 4.1.2 docs.

The definition given in the Intel documentation allows only for the use of the types int, long, long long as well as their unsigned counterparts. GCC will allow any integral scalar or pointer type that is 1, 2, 4 or 8 bytes in length.

So it would seem you cannot use this function to handle that case.

于 2012-02-13T00:33:00.613 回答
2

You are passing a char * to __sync_bool_compare_and_swap. Assuming your char arrays (all three of them!) are properly aligned to 64 bits (if they're allocated in the way you show, they may not be - use malloc!), try casting to (long long *) before passing to __sync_bool_compare_and_swap. Failing that, use inline assembler and invoke CMPXCHG8B directly.

于 2012-02-13T00:30:27.617 回答