3 ms·
Article talks about two different types locks, one is the mutex, which the article admits won’t work when using with multiple instances. The other is briefly m
by tored 6y ago
Article talks about two different types locks, one is the mutex, which the article admits won’t work when using with multiple instances.
The other is briefly mentioned with a link to Wikipedia and is a optimistic lock that is to be used with your database.
I talked about two use cases for optimistic locking, one applied with a database, that with will work with any number of instances, because the synchronization check lives in the database and not in the instance.
My other case was to use an optimistic lock instead of the mutex example, that will still only work with single instance because the synchronization check is local to the instance.
(Edit: wrong, it could actually work there too, if saveBalance() was changed)
Optimistic locking is quite simple to implement, just check that the value has not changed before writing, if it has you have to reread the value again.