gcc 原子操作函数
来源:互联网 发布:网络怎么买彩票 编辑:程序博客网 时间:2024/05/16 14:19
gcc从4.1.2以后提供了__sync_*系列的内嵌函数,提供用于针对数字或布尔型变量的原子操作。
1、原子操作的api函数
(1)直接操作数的原子操作
第一组返回更新前的值,第二组返回更新后的值
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, ...)
type可以是1,2,4或8字节长度的int类型,即:
int16_t / uint16_t
int32_t / uint32_t
int64_t / uint64_t
(2)比较后操作数的原子操作
type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
这两个函数提供原子的比较和交换,如果*ptr == oldval,就将newval写入*ptr,
第一个函数在相等并写入的情况下返回true.
第二个函数在返回操作之前的值。
(3)其他原子操作
type __sync_lock_test_and_set (type *ptr, type value, ...)
将*ptr设为value并返回*ptr操作之前的值。
void __sync_lock_release (type *ptr, ...)
将*ptr置0
2、内存栅障
(1)内存栅障介绍
内存栅障主要是处理不同cpu运作时(主要是执行不同线程代码时),一个cpu对内存的操作的原子性,也就保证其他cpu见到的内存单元数据的正确性。
(2)内存栅障应用
对于执行一条指令,操作到4个寄存器时,如:
write1(dev.register_size,size);
write1(dev.register_addr,addr);
write1(dev.register_cmd,READ);
write1(dev.register_control,GO);
最后一个寄存器是控制寄存器,在所有的参数都设置好之后向其发出指令,设备开始读取参数.
如果最后一条write1被换到了前几条语句之前,那么肯定不是我们所期望的,这时候我们可以在最后一条语句之前加入一个memory barrier,强制cpu执行完前面的写入以后再执行最后一条:
write1(dev.register_addr,addr);
write1(dev.register_cmd,READ);
__sync_synchronize();
write1(dev.register_control,GO);
memory barrier有几种类型:
acquire barrier : 不允许将barrier之后的内存读取指令移到barrier之前(linux kernel中的wmb())。
release barrier : 不允许将barrier之前的内存读取指令移到barrier之后 (linux kernel中的rmb())。
full barrier : 以上两种barrier的合集(linux kernel中的mb())。
3、原子操作应用范围
原子操作只允许一次更新或读一个内存单元。 需要原子地更新多个单元时, 就必须使用锁来代替它了。
例如, 如果需要更新两个相互关联的计数器时, 就必须使用锁, 而不是两次单独的原子操作了。
4、原子操作例子
#include <stdio.h>#include <pthread.h>#include <stdlib.h>static int count = 0;void *test_func(void *arg){ int i=0; for(i=0;i<20000;++i){ __sync_fetch_and_add(&count,1); } return NULL;}int main(int argc, const char *argv[]){ pthread_t id[20]; int i = 0; for(i=0;i<20;++i){ pthread_create(&id[i],NULL,test_func,NULL); } for(i=0;i<20;++i){ pthread_join(id[i],NULL); } printf("%d\n",count); return 0;}
5、原子操作封装使用
//原子操作//设置值int lock_set(volatile int &a, int value){__sync_val_compare_and_swap(&a, a, value);return a;}//加1int lock_inc(volatile int &n){return __sync_fetch_and_add(&n, 1);}//减1int lock_dec(volatile int &n){return __sync_fetch_and_sub(&n, 1);}//加值valueint lock_add(volatile int &n, int value){return __sync_fetch_and_add(&n, value);}//减值valueint lock_sub(volatile int &n, int value){return __sync_fetch_and_sub(&n, value);}//位或valueint lock_or(volatile int &n, int value){return __sync_fetch_and_or(&n, value);}//位与valueint lock_and(volatile int &n, int value){return __sync_fetch_and_and(&n, value);}//异或valueint lock_xor(volatile int &n, int value){return __sync_fetch_and_xor(&n, value);}//无符号类型(函数重载)//设置值unsigned int lock_set(volatile unsigned int &a, unsigned int value){__sync_val_compare_and_swap(&a, a, value);return a;}//加1unsigned int lock_inc(volatile unsigned int &n){return __sync_fetch_and_add(&n, 1);}//减1unsigned int lock_dec(volatile unsigned int &n){return __sync_fetch_and_sub(&n, 1);}//加值valueunsigned int lock_add(volatile unsigned int &n, unsigned int value){return __sync_fetch_and_add((int*)&n, value);}//减值valueunsigned int lock_sub(volatile unsigned int &n, unsigned int value){return __sync_fetch_and_sub((int*)&n, value);}//位或valueunsigned int lock_or(volatile unsigned int &n, unsigned int value){return __sync_fetch_and_or((int*)&n, value);}//位与valueunsigned int lock_and(volatile unsigned int &n, unsigned int value){return __sync_fetch_and_and((int*)&n, value);}//异或valueunsigned int lock_xor(volatile unsigned int &n, unsigned int value){return __sync_fetch_and_xor((int*)&n, value);}
- gcc 原子操作函数
- gcc内置原子操作函数
- linux gcc原子操作
- gcc的原子操作
- gcc原子化操作
- gcc 原子操作
- GCC的原子操作
- gcc 原子操作
- 【转】Linux下GCC内置原子操作函数
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- GCC 提供的原子操作
- gcc提供的原子操作
- GCC原子操作(Atomic Builtins)
- GCC 提供的原子操作
- GCC内建原子操作
- lockfree 的队列的实现
- 02_Struts2_namespace
- 企业中,关于领导者
- 自定义漂亮的圆形进度条
- jvm垃圾回收
- gcc 原子操作函数
- Java虚拟机内存溢出测试
- JavaScript图像处理(2) - 亮度对比度调整(Brightness/Contrast)
- java垃圾回收之Minor GC和Major GC
- 在java中使用kryo框架来实现高效序列化与反序列化
- Ubuntu创建桌面启动器的两种方法
- Linux下map hash_map和unordered_map效率比较
- 今日学习C
- 基于spring实现的restful webservice返回json数据