5 ms·
- Or if you have to hold several locks, grab them all atomically and if even one grab fails, release them all and try again later. - If you really need all the
by dreamcompiler 8y ago
- Or if you have to hold several locks, grab them all atomically and if even one grab fails, release them all and try again later.
- If you really need all the locks right now, steal the ones you don't own from another thread, and make sure all threads can deal with lock theft.
It's not always easy to sort out the best way to deal with deadlocks. Each approach has significant tradeoffs.
- kazinator 8y agoMultiple locks are not always in the same scope. Method Foo::Update acquires a lock, then without releasing the lock calls into another class/object where Bar::Commit also acquires its own lock.
- dreamcompiler 8y agoYep. That's certainly one of the tradeoffs.
- pkaye 8y agoHow would you do this in Go? If not Go, what language has the capability?
- gatestone 8y ago"...shared values are passed around on channels and, in fact, never actively shared by separate threads of execution. Only one goroutine has access to the value at any given time. Data races cannot occur, by design." [...if you stick to this style of programming...]