gcc内置原子操做函数

    最近在用户态下忽然须要用到原子变量,又不想本身编译boost,思来索去,无心中竟发现gcc还有这一组内置函数.html

//原子操做,返回变化前的值
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);

按照官方文档的说法,ide

GCC will allow any integral scalar or pointer type that is 1, 2, 4, or 8 bytes in length. 16-byte integral types are also allowed if `__int128' (see __int128) is supported by the architecture.函数

也就是说至少支持长度为 1,2,4,8的整形。16位是否支持取决于体系结构测试


简单的测试了一下,发现不用include任何头文件,gcc编译时会自动替换成对应的汇编代码。fetch

int main()
{
    int a = 100;
    __sync_fetch_and_add(&a,1);
    return a;
}

看了下生成的汇编是这样的:ui

    .file   "funtest.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16 
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $100, -4(%rbp)
    lock addl   $1, -4(%rbp)                //__sync_fetch_and_add(&a,1);对应的指令
    movl    -4(%rbp), %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret 
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Debian 4.7.2-5) 4.7.2"
    .section    .note.GNU-stack,"",@progbits


参考连接atom

https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/_005f_005fatomic-Builtins.html#_005f_005fatomic-Builtinsspa

相关文章
相关标签/搜索