Atomic memory access

1 篇文章 0 订阅

Gcc 提供了一些built in的函数,以实现内存访问的原子性:具体函数在 http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

In most cases, these builtins are considered a full barrier. That is, no memory operand will be moved across the operation, either forward or backward. Further, instructions will be issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation. 

full barrier 就是说明了这些函数是通过cpu以memory barrier方式访问内存的,所以即使在多核CPU下,这些函数也可保证其原子性. 

也就隐含的说明了,不管是单进程还是多进程只要是对函数中ptr参数的内存地址的访问,都是原子进行的——如ptr是在共享内存中,那多个进程对ptr所指向的内存的访问都是原子进行的,保证了在多个进程间的同步,即使在多核cpu下.

Memory barrier: http://en.wikipedia.org/wiki/Memory_barrier

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值