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

前端 未结 2 2165
时光说笑
时光说笑 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 15:00

    The C++ standard makes no guarantee that std::atomic operations are lock-free. However, you can use std::atomic::is_lock_free() to find out if the operation of std::atomic is lock free 29.6.5 [atomics.types.operations.req] paragraph 7:

    Returns: True if the object’s operations are lock-free, false otherwise.

    If it is not lock-free it will still do the required synchronization but it uses some lock to do so.

提交回复
热议问题