Why is std::atomic much slower than volatile bool?

后端 未结 3 562
走了就别回头了
走了就别回头了 2021-01-31 03:24

I\'ve been using volatile bool for years for thread execution control and it worked fine

// in my class declaration
volatile bool stop_;

-----------------

// I         


        
3条回答
  •  执笔经年
    2021-01-31 03:59

    Code from "Olaf Dietsche"

     USE ATOMIC
     real   0m1.958s
     user   0m1.957s
     sys    0m0.000s
    
     USE VOLATILE
     real   0m1.966s
     user   0m1.953s
     sys    0m0.010s
    

    IF YOU ARE USING GCC SMALLER 4.7

    http://gcc.gnu.org/gcc-4.7/changes.html

    Support for atomic operations specifying the C++11/C11 memory model has been added. These new __atomic routines replace the existing __sync built-in routines.

    Atomic support is also available for memory blocks. Lock-free instructions will be used if a memory block is the same size and alignment as a supported integer type. Atomic operations which do not have lock-free support are left as function calls. A set of library functions is available on the GCC atomic wiki in the "External Atomics Library" section.

    So yeah .. only solution is to upgrade to GCC 4.7

提交回复
热议问题