Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>"Better" depends on context. They are "equally powerful" according to James McParlane. I highly recommend viewing <a href="http://blog.metawrap.com/2004/11/09/semaphore-vs-monitor/" rel="nofollow noreferrer">his blog for a discussion on the differences</a>.</p> <p>Here is a quick guide I found:</p> <p><strong>Semaphores</strong></p> <ul> <li>Can be used anywhere in a program, but should not be used in a monitor </li> <li><code>Wait()</code> does not always block the caller (i.e., when the semaphore counter is greater than zero).</li> <li><code>Signal()</code> either releases a blocked thread, if there is one, or increases the semaphore counter.</li> <li>If <code>Signal()</code> releases a blocked thread, the caller and the released thread both continue.</li> </ul> <p><strong>Condition Variables</strong></p> <ul> <li>Can only be used in monitors</li> <li><code>Wait()</code> always blocks the caller.</li> <li><code>Signal()</code> either releases a blocked thread, if there is one, or the signal is lost as if it never happens.</li> <li>If <code>Signal()</code> 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.</li> </ul> <p>This information from: <a href="http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MONITOR/sema-vs-monitor.html" rel="nofollow noreferrer">http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MONITOR/sema-vs-monitor.html</a></p> <p>Some useful resources:</p> <ul> <li><a href="https://stackoverflow.com/questions/34519/what-is-a-semaphore">What is a Semaphore</a></li> <li><a href="https://stackoverflow.com/questions/836301/looking-for-good-analogy-examples-for-monitor-verses-semaphore">Looking for Good Analogy Examples for Monitor Versus Semaphore</a></li> <li><a href="https://stackoverflow.com/questions/301160/what-are-the-differences-between-various-threading-synchronization-options-in-c">What are the differences between various threading synchronization options in C#?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload