Background: Know: spinLock , access by multiple managers, Recognize:

Tag: eg

Previous Next

spinLock example

The spinLock is a kind of locks locks which is designed to prevent more than two processors processing simultaneously. The spinLock can only support one manager using, if a manager try to use a spinLock which is already hold by other manager, the requirement that he required will keep loop to waiting until the spinLock released. If the spinLock has not been hold already, the requirement can get in and process immediately. The spinLock can prevent more than one manager getting in at the same time at any moment, so this lock can avoid many processors simultaneously running and competing sharing resources effectively.

In the graph, it shows the "spinLock" mechanism. As we can see, when manager 1 requires "set" when spinlock is "notInUse", then he can tries to write value, and as there are no other manager got in first, the value is incremented to inUse. After manager 1 require "set " again, spinLock is released and turns to be "notInUse" from "inUse". For the second manager, when he send "set" require, "spinLock" is at "InUse", so he have to waiting until that "spinLock" is "notInUse", then, after "spinLock" becomes "notInUse", and no other manager gets in, so manager 2 can read object and write value, the process is as same as that of manager 1. For the third manager, when he send "set " require, the "spinLock" is at "InUse", so his requirement have to been wait and looped requiring. Then, the "spinLock" is at "notInUse", while manager 2 has already get in, so he receive error. After that, "spinLock" is at "InUse", so manager 3 have to still waiting "spinLock" is at "notInUse" and no manager already got in, and finally, he can use it, and the using process is as same as that of manager 1.