用户态的原子变量

GCC(GNU Compiler Collection)内建原子操作函数说明
6.51 Legacy __sync Built-in Functions for Atomic Memory Access

编译器内建,可以代替多线程全局变量自加、自减等操作,并且效率高于使用线程锁。
type为8字节以下的整形数

函数(加、减、与、或、非、异或共12个

type __sync_fetch_and_add (type *ptr, typevalue, ...)

type __sync_fetch_and_sub (type *ptr, typevalue, ...)

type__sync_fetch_and_or (type *ptr, typevalue, ...)

type __sync_fetch_and_and (type *ptr, typevalue, ...)

type __sync_fetch_and_xor (type *ptr, typevalue, ...)

type __sync_fetch_and_nand (type *ptr, typevalue, ...)

注意:GCC 4.4及更高版本__sync_fetch_and_nand*ptr = ~(tmp &value);之前版本是:*ptr = ~tmp & value

type __sync_add_and_fetch (type *ptr, typevalue, ...)

type __sync_sub_and_fetch (type *ptr, typevalue, ...)

type __sync_or_and_fetch (type *ptr, typevalue, ...)

type __sync_and_and_fetch (type *ptr, typevalue, ...)

type __sync_xor_and_fetch (type *ptr, typevalue, ...)

type __sync_nand_and_fetch (type *ptr, typevalue, ...)

注意:GCC 4.4及更高版本__sync_nand_and_fetch*ptr = ~(tmp & value);之前版本是:*ptr = ~tmp & value


bool__sync_bool_compare_and_swap (type *ptr, type oldval, type newval, ...)
如果ptr = ptr == oldval ? newval :oldval,成功返回1。
type__sync_val_compare_and_swap (type *ptr, type oldval, type newval, ...)
同上,成功返回ptr替换之前的值。


__sync_synchronize(...)
发出一个full memory barrier内存屏障,作用是该屏障之前的指令不能移动到屏障下边执行,下边的指令也不可移动到上面执行。

type__sync_lock_test_and_set (type *ptr, type value, ...)

将ptr赋值为value

void__sync_lock_release (type *ptr, ...)
将ptr赋值为0

示例程序如下:
运行环境gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)


  1. #include <stdio.h>
  2. #include <stdint.h>

  3. int main(int argc, char **argv) {
  4.         uint64_t i = 8 ;

  5.         setvbuf(stdout, NULL, _IONBF, 0) ;

  6.         fprintf(stdout, "i = 8[%lu]\n", __sync_fetch_and_add(&i, 1)) ;
  7.         fprintf(stdout, "i = 9[%lu]\n", __sync_fetch_and_sub(&i, 1)) ;
  8.         fprintf(stdout, "i = 8[%lu]\n", __sync_fetch_and_or(&i, 1)) ;
  9.         fprintf(stdout, "i = 9[%lu]\n", __sync_fetch_and_and(&i, 1)) ;
  10.         fprintf(stdout, "i = 1[%lu]\n", __sync_fetch_and_xor(&i, 1)) ;
  11.         fprintf(stdout, "i = 0[%lu]\n", __sync_fetch_and_nand(&i, 1)) ; /*i = ~(i&1) */
  12.         fprintf(stdout, "i = 18446744073709551615[%lu]\n", i) ;

  13.         i = 8 ;
  14.         fprintf(stdout, "i = 9[%lu]\n", __sync_add_and_fetch(&i, 1)) ;
  15.         fprintf(stdout, "i = 8[%lu]\n", __sync_sub_and_fetch(&i, 1)) ;
  16.         fprintf(stdout, "i = 9[%lu]\n", __sync_or_and_fetch(&i, 1)) ;
  17.         fprintf(stdout, "i = 1[%lu]\n", __sync_and_and_fetch(&i, 1)) ;
  18.         fprintf(stdout, "i = 0[%lu]\n", __sync_xor_and_fetch(&i, 1)) ;
  19.         fprintf(stdout, "i = -1[%ld]\n", __sync_nand_and_fetch(&i, 1)) ;
  20.         fprintf(stdout, "i = 18446744073709551615[%lu]\n", i) ;

  21.         i = 8 ;
  22.         uint64_t j = 0 ;
  23.         fprintf(stdout, "[%d]\n", __sync_bool_compare_and_swap(&i, 8, 2)) ;
  24.         fprintf(stdout, "i = 2[%lu]\n", i) ;

  25.         j = __sync_val_compare_and_swap (&i, 2, 8) ;
  26.         fprintf(stdout, "i = 8[%lu]\n", i) ;
  27.         fprintf(stdout, "j = 2[%lu]\n", j) ;

  28.         __sync_synchronize() ;

  29.         i = __sync_lock_test_and_set (&j, 10) ;
  30.         fprintf(stdout, "i = 2[%lu]\n", i) ;
  31.         fprintf(stdout, "j = 10[%lu]\n", j) ;

  32.         __sync_lock_release (&i) ;
  33.         __sync_lock_release (&j) ;
  34.         fprintf(stdout, "i = 0[%lu]\n", i) ;
  35.         fprintf(stdout, "j = 0[%lu]\n", j) ;

  36.         return 0 ;
  37. }

详细信息详见GCC文档 6.51 6.52 6.53 6.54 6.55章节
网址: http://gcc.gnu.org/onlinedocs/gcc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值