-
Mutex.lock
=Monitor.enter
-
Mutex.unlock
=Monitor.exit
Monitor is Mutex can be Reentrant. Reentrancy example Pull Request. Reentrant means locks are acquired per-thread instead of per-usage. Source code can be found here.
Monitor is possible to have nested synchornize
loop like this:
require "monitor"
monitor = Monitor.new
monitor.synchornize do
monitor.synchornize do
...
end
end
If you use Mutex
will result in deadlock:
mutex = Mutex.new
mutex.synchornize do
mutex.synchornize do
...
end
end
The performance gain of using Mutex
is almost always neglectable, so it seems like a good choice to avoid a serious deadlock issue to use Monitor when you don’t know if the code you wrap also has synchornize
block inside it or not.
See also: Mutex.