Re: Semaphores and .NET
- From: "Lebesgue" <nospam@xxxxxxx>
- Date: Wed, 24 Aug 2005 12:07:40 +0200
"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.
.
- Follow-Ups:
- Re: Semaphores and .NET
- From: William Stacey [MVP]
- Re: Semaphores and .NET
- From: Nigel Norris
- Re: Semaphores and .NET
- References:
- Semaphores and .NET
- From: jinksk
- Re: Semaphores and .NET
- From: William Stacey [MVP]
- Re: Semaphores and .NET
- From: Lebesgue
- Re: Semaphores and .NET
- From: William Stacey [MVP]
- Semaphores and .NET
- Prev by Date: Re: Char... Unicode version (bug?): what about 2.0?
- Next by Date: RE: ActiveX type control or similar in C#
- Previous by thread: Re: Semaphores and .NET
- Next by thread: Re: Semaphores and .NET
- Index(es):
Relevant Pages
|