Where is the lock for a std::atomic?

前端 未结 3 1558
轻奢々
轻奢々 2020-11-27 04:21

If a data structure has multiple elements in it, the atomic version of it cannot (always) be lock-free. I was told that this is true for larger types because the CPU can not

3条回答
  •  渐次进展
    2020-11-27 05:02

    From 29.5.9 of the C++ standard:

    Note: The representation of an atomic specialization need not have the same size as its corresponding argument type. Specializations should have the same size whenever possible, as this reduces the effort required to port existing code. — end note

    It is preferable to make the size of an atomic the same as the size of its argument type, although not necessary. The way to achieve this is by either avoiding locks or by storing the locks in a separate structure. As the other answers have already explained clearly, a hash table is used to hold all the locks. This is the most memory efficient way of storing any number of locks for all the atomic objects in use.

提交回复
热议问题