Re: Semaphores and .NET

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"William Stacey [MVP]" <staceyw@xxxxxxxx> wrote in message
news:%23DoaYNEqFHA.3720@xxxxxxxxxxxxxxxxxxxxxxx
> As I said, he could modify it pretty easy. Instead of monitor, use a
named
> mutex and shared file to hold the count or mem mapped file, etc. Mind
> sharing what deadlock problem there is? I would be happy to fix it. TIA.
>
> --
> William Stacey [MVP]

As stated in the discussion under your article:
(http://www.codeproject.com/csharp/DijkstraCountingSemaphore.asp#xx1185086xx
)

public bool Acquire(int millisecondsTimeout)
{
lock(syncLock)
{
...snip...
Monitor.Wait(syncLock, millisecondsTimeout)
...snip...
}
}
public void Release(int releaseCount)
{
...snip...
lock(syncLock)
{
...snip...
Monitor.PulseAll(syncLock);
...snip...
}
}



To aquire a lock on the semaphore, you need to obtain syncLock (which is
released the moment you leave the function). If there are no slots
available, you block at the Monitor.Wait() call, INSIDE the lock statement.
When a different thread finishes up and tries to release, it attempts to
aquire the exclusive lock on syncLock. Since there is a thread blocking on
Monitor.Wait() inside the lock, this won't release.



.



Relevant Pages

  • Re: Semaphores and .NET
    ... Monitor.Wait releases the lock. ... >> mutex and shared file to hold the count or mem mapped file, ... > aquire the exclusive lock on syncLock. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Semaphores and .NET
    ... known pattern in this type of producer-consumer locking patterns. ... Monitor.Wait releases the lock so a thread calling Release is not blocked. ... > aquire the exclusive lock on syncLock. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: threading - Monitor.Wait/Pulse
    ... don't get with WaitHandles is that it releases the Lock on the Object you ... You would probably also need to use either a WaitHandle, ... End SyncLock ... will thread 3 then get the resource or will thread 1 ...
    (microsoft.public.dotnet.general)
  • Re: threading - Monitor.Wait/Pulse
    ... don't get with WaitHandles is that it releases the Lock on the Object you ... If I break the SyncLock into two groups then the memebers of the objTxPacket ... The WaitHandle, I'am guessing, seems to ... will thread 3 then get the resource or will thread 1 ...
    (microsoft.public.dotnet.general)
  • Re: Vector (was Re: Change character in string)
    ... already has it's first fork when .equalsis invoked ... so it's too late for grabbing the napkin now. ... each method would have to aquire Collections.synchronizedCollection's ... central lock - and that has a hidden price tag that may really hurt. ...
    (comp.lang.java.programmer)