atomiclock-freemutual-exclusion

If Atomic is used, is there a lock contention


This is not specific to any language. Just want to know the "lock contention" terms better.

Let us say: we want to execute two kinds of commands using ARM instructions

We have 10 stores, each sells product, there is one central boss holding a ledger recording the total product sold.

If we are using exclusive, my image is: the 10 stores say I want to update the sum, the BOSS gives the ledger to one of the stores, he adds the products sold to the sum. Then the boss gives the ledger to the next store. they all take turns so the sum is not screwed up.

If we are using atomic, my image is: 10 stores just write a letter to the BOSS about how many products they sold, the BOSS will do the sum by himself one by one. There is no permission granted to any store etc. The sum is just doing AtomicAdd for 10 stores. So in this case, what is the lock to be contended with?


Solution

  • The atomic is not handling the concepts that you are looking at. Lets say one of the stores has a very large product count and a high volume count. The store is in Gotham City and has lots of customers and employees. The regional boss yells 'STOP' to add up the ledgers. Unfortunately, the employee updating the Gotham City ledge was changing 999,999 to 1,000,000 and the number reads as 1,000,999.

    If the regional boss can not yell 'STOP', then employees at other stores can be altering the count while the inventory is being computed. If stores exchange inventory, this can lead to inaccuracy. Also, you will never know the actual sales at some time as between the first store and last store count to be accessed, you have sales at all the stores which will alter the accuracy of the sum.

    Now, lets say there are regional bosses and a national boss. The national boss tries two ways to calculate all of the sales. She sums the count for all the regional bosses and conducts a full count of all the stores. These numbers will not be equal unless she yells 'STOP' to ensure no sales are made and each regional managers can conduct a regional count. She may conduct the total national count at the same time as the underlying process has stopped. Unfortunately, the business goes bankrupt due to the time this takes and the lost sales it created.


    The business reforms and they decide to count only one region at a time. Either a regional boss or a national boss may yell 'STOP' for that region. The lock contention happens when two bosses yell 'STOP' during the same time. Also, how do the sales restart? A boss has to yell 'GO'. If two bosses are doing a sales count at the same time, the stores will start reselling as soon as the first boss to finish yells 'GO'. This leads to inaccurate inventory counts (as well as counting only one local region at a time) and the business goes bankrupt because they do not have product to sell and customers get frustrated and go elsewhere.


    The company reforms and they decide that the sales people will update a regional count and a national count every time a sale is made. As the national manager has seen some problems before, she thinks a long time about how the sales people will update these counts. They buy a machine that will add one to the counts every time a button is pressed. As she knows that if the employee has to look at the count, the regional and national number may change as they are calculating the new number.


    The 2nd case has lock contention issues. Locks are yelling 'STOP' and 'GO' and you have contention when multiple actors/processes want to use that lock at the same time. All cases an issue with atomic. This is the ability to update information so that it is consistent when other read (or read update) the value. In the last case, the machine gave a 'lock-free' primitive to the business so they can update the count atomically without needing a lock.

    The locks are needed when you have calculations that involve multiple atomic values or mixtures of reading and writing. If the value is not atomic to begin with, then even the locks won't work.