First of all, you are perfectly allowed to have something like std::atomic<very_nontrivial_large_structure>
, so std::atomic
as such cannot generally be guaranteed to be lock-free (although most specializations for trivial types like bool
or int
probably could, on most systems). But that is somewhat unrelated.
The exact reasoning why atomic_flag
and nothing else must be lock-free is given in the Note in N2427/29.3:
Hence the operations must be address-free. No other type requires lock-free operations, and hence the atomic_flag type is the minimum hardware-implemented type needed to conform to this standard. The remaining types can be emulated with atomic_flag, though with less than ideal properties.
In other words, it's the minimum thing that must be guaranteed on every platform, so it's possible to implement the standard correctly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…