Is it better to synchronize with semaphores or with monitors?
"Better" depends on context. They are "equally powerful" according to James McParlane. I highly recommend viewing his blog for a discussion on the differences.
Here is a quick guide I found:
Semaphores
Wait()
does not always block the caller (i.e., when the semaphore counter is greater than zero).Signal()
either releases a blocked thread, if there is one, or increases the semaphore counter.Signal()
releases a blocked thread, the caller and the released thread both continue.Condition Variables
Wait()
always blocks the caller.Signal()
either releases a blocked thread, if there is one, or the signal is lost as if it never happens.Signal()
releases a blocked thread, the caller yields the monitor (Hoare type) or continues (Mesa Type). Only one of the caller or the released thread can continue, but not both.This information from: http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MONITOR/sema-vs-monitor.html
Some useful resources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With