代码改变世界

Linux下原子性操作,类似Windows下的InterLockedXXX

2015-06-23 16:57  哦万里云  阅读(863)  评论(0编辑  收藏  举报

 

其中,这些操作的操作数(type

可以是1,2,4或8字节长度的int类型,即:

int8_t / uint8_t

int16_t / uint16_t

int32_t / uint32_t

int64_t / uint64_t

type __sync_fetch_and_add (type *ptr, type value);
type __sync_fetch_and_sub (type *ptr, type value);
type __sync_fetch_and_or (type *ptr, type value);
type __sync_fetch_and_and (type *ptr, type value);
type __sync_fetch_and_xor (type *ptr, type value);
type __sync_fetch_and_nand (type *ptr, type value);
type __sync_add_and_fetch (type *ptr, type value);
type __sync_sub_and_fetch (type *ptr, type value);
type __sync_or_and_fetch (type *ptr, type value);
type __sync_and_and_fetch (type *ptr, type value);
type __sync_xor_and_fetch (type *ptr, type value);
type __sync_nand_and_fetch (type *ptr, type value);

 

PS:

bool __sync_bool_compare_and_swap (type *ptr, type cmpValue,type newval)

type __sync_val_compare_and_swap (type *ptr, type cmpValue,type newval)



type __sync_lock_test_and_set (type *ptr, type value)

void __sync_lock_release (type *ptr)

参考:

《Multithreaded simple data type access and atomic variables》

《Built-in functions for atomic memory access》