This algorithm is modeled on a waiting room with an entry and exit doorway. Initially the entry door is open and the exit door is closed. All processes which request entry into the critical section at the same time enter the waiting room; the last of them closes the entry door and opens the exit door. The processes then enter the critical section one by one. The last process to leave the critical section closes the exit door and reopens the entry door so he next batch of processes may enter.
Install the gem or add it to the Gemfile.
$ gem install szymanskis_mutex
Provide a block containing the critical section to the class method
mutual_exclusion(concern)
. The parameter concern
is used so that
you can have different use cases without them accessing the same variables.
(You can have as many separate MutEx as you want.)
class RaceConditionReproducer
@@value = 0
def mutex_increment
SzymanskisMutex.mutual_exclusion(:add) { unsafe_increment }
end
def unsafe_increment
sleep 2
temp = @@value
sleep 2
temp += 1
sleep 2
@@value = temp
end
end
The MutEx will work across different instances of the same class as it works with class variables.
Pull Requests will be reviewed before merging. Issues are being addressed. All Pull Requests must contain specs for the changes.