2

I've known that std::atomic_flag is guaranteed to be lock free whereas other atomic stuffs, such as std::atomic<int>, std::atomic<bool> are not, meaning that they may be generated with lock.

My question is why there is only one thing std::atomic_flag is lock free and others may be not? Why don't we force to use lock free to generate things like std::atomic<bool>? Is there some technique reason?

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Yves
  • 10,315
  • 11
  • 69
  • 141
  • The simple answer: If on a given machine a sequence of instructions is needed to perform the action on T it must be guaranteed that these action sequence is not interruptible. Typically a machine can not guarantee a sequence of instructions can not be interrupted without a lock. So only these operations on T are lock free which can be managed in a single assembly instruction. So the reason is: Hardware! :-) Some cores support things like "test bit and set" or manipulate a 4 or 8 byte int in a single instruction (read-modify-write) but a lot of cores can not do this within a single instruction! – Klaus Dec 12 '19 at 10:09

0 Answers0