A couple of months ago, I was developing services to calculate balance transaction sent from one’s user account to another’s user account and vice versa. Each transaction request is handled concurrently, thus reading and writing to databases also happens concurrently. Concurrent operations could cause data race if handled incorrectly and causing indeterminate value in user’s balance calculation. To avoid this, we need to make balance calculation on each user’s account run synchronously.
Hi, thanks for publishing your solution.
I think there is a bug in your code as @michaelseu2011 pointed out.
I made some improvement for multi lock,
here is my code.
I use internal lock to make sure acquiring and releasing the lock run synchronously.
I also made some benchmarking to compare the performance between lock from mutex and multilock implementation.
The result is that multi lock is not effective for small operation since it cause overhead. But for heavy operation, you can use multilock implementation.
Hope it helps.