Hi all,<br><br><div class="gmail_quote">On 12 March 2010 02:31, Tim Golden <span dir="ltr">&lt;<a href="mailto:mail@timgolden.me.uk">mail@timgolden.me.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
<div class="im"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
The advantage of mutex&#39;s over semaphores would be that applications that<br>
terminate abnormally would have their mutex released, while applications<br>
using semaphors that terminated abnormally would leave their semaphore<br>
with an incorrect count?<br>
</blockquote>
<br></div>
See above; I don&#39;t this mutexes and semaphores differ in this respect.<br></blockquote></div><br><br>Actually, if you take the example of someone calling TerminateProcess() against the process (or some other equally fatal do-not-pass-the-finally-block abnormal exits), they will be different.<br>
<br>In this case, a mutex will be unlocked, and another process&#39; waiting thread 
will get a WAIT_ABANDONED wake up.<br><br>For semaphores, the handles will be mopped up, but the available count on that semaphore will not be incremented.  So if you&#39;ve got lots of other processes coming and being killed in this way, you can easily run out of slots, even though there is only one handle open on the thing.<br>
<br>It&#39;s a pity, since the semaphore way of doing this is much cleaner and faster than the &quot;bunch of mutexes&quot; method.<br><br>If you&#39;ve got some other way to guarantee that you&#39;ll see such processes vanish (eg. grab a SYNCHRONIZE handle on it via OpenProcess() -- these will get signalled when the process dies normally or abnormally), then it&#39;s still workable, since you can then call ReleaseSemaphore() yourself -- without having to wait for the original holder to do it.  But now you&#39;ve got two problems (limit instances *and* notice when they die). :)<br>
<br><br>Or, maybe it&#39;s an acceptable risk.  How badly do you want to fight TerminateProcess / death by Task Manager anyway?  And you could argue that other kinds of abnormal explosions are bugs that need fixing.  My vote would definitely be for the semaphore method.    <br>
<br><br>Cheers,<br>Chris.<br><br><br><br>