Does the C++ 11 standard guarantees that std::atomic<> is implemented as a lock-free operation?

前端 未结 2 2175
时光说笑
时光说笑 2021-02-18 14:30

I\'m at a junction, I\'m trying to pick one between mutex-lock-based data structure and lock-free ( and possibly wait-free ) data structure.

While digging a little deepe

2条回答
  •  后悔当初
    2021-02-18 14:53

    If by atomic you mean, using hardware support without locks, then yes, the standard doesn't give you a guarantee for that. Why? Well, because different architectures support different kind of hardware atomicity. std::atomic<> has the handy is_lock_free() method which can be used to check wether the given object is actually lock free, or uses a lock internally to guarantee atomic operations. You can use that and check on your target hardware wether it would be lock free or not, and then decide what data structure to go for.

    However, that being said, if the target architecture has hardware support for atomic operations for the fixed width integrals you are interested in, and you didn't obtain your copy of the standard library from the the shady software shop in the ghetto, it's probably going to use the hardware instead of a full blown lock.

提交回复
热议问题