Will atomic operations block other threads?

前端 未结 5 381
难免孤独
难免孤独 2021-02-01 10:53

I am trying to make \"atomic vs non atomic\" concept settled in my mind. My first problem is I could not find \"real-life analogy\" on that. Like customer/restaurant relationshi

5条回答
  •  执笔经年
    2021-02-01 11:26

    Being "atomic" is an attribute that applies to an operation which is enforced by the implementation (either the hardware or the compiler, generally speaking). For a real-life analogy, look to systems requiring transactions, such as bank accounts. A transfer from one account to another involves a withdrawal from one account and a deposit to another, but generally these should be performed atomically - there is no time when the money has been withdrawn but not yet deposited, or vice versa.

    So, continuing the analogy for your question:

    What is the meaning of "no other thread can observe the modification half-complete"?

    This means that no thread could observe the two accounts in a state where the withdrawal had been made from one account but it had not been deposited in another.

    In machine terms, it means that an atomic read of a value in one thread will not see a value with some bits from before an atomic write by another thread, and some bits from after the same write operation. Various operations more complex than just a single read or write can also be atomic: for instance, "compare and swap" is a commonly implemented atomic operation that checks the value of a variable, compares it to a second value, and replaces it with another value if the compared values were equal, atomically - so for instance, if the comparison succeeds, it is not possible for another thread to write a different value in between the compare and the swap parts of the operation. Any write by another thread will either be performed wholly before or wholly after the atomic compare-and-swap.

    The title to your question is:

    Will atomic operations block other threads?

    In the usual meaning of "block", the answer is no; an atomic operation in one thread won't by itself cause execution to stop in another thread, although it may cause a livelock situation or otherwise prevent progress.

    That means thread will wait until atomic operation is done?

    Conceptually, it means that they will never need to wait. The operation is either done, or not done; it is never halfway done. In practice, atomic operations can be implemented using mutexes, at a significant performance cost. Many (if not most) modern processors support various atomic primitives at the hardware level.

    Also if above statement is true, do all atomic operations are thread-safe?

    If you compose atomic operations, they are no longer atomic. That is, I can do one atomic compare-and-swap operation followed by another, and the two compare-and-swaps will individually be atomic, but they are divisible. Thus you can still have concurrency errors.

提交回复
热议问题