Should 64bit Compare&Swap (CAS) work on a 32bit machine? (or 64bit machine?)

余生颓废 提交于 2019-12-07 15:40:37

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.

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!