Home All Groups Group Topic Archive Search About

Two threads are accessing my Synclocked code

Author
6 Sep 2006 5:51 PM
Joel Moore
I'm looking at my debugger right now and can clearly see that two threads
have their execution pointer residing within the same Synclock block.

One thread is executing a Monitor.Wait().  I know this suspends the thread
but I didn't think this would open the gate for other threads to enter the
block.

Any ideas what could be happening?

Joel Moore

Author
6 Sep 2006 6:11 PM
Joel Moore
Joel Moore <asdads***@asdaadad.com> wrote in
Show quoteHide quote
news:Xns98368CE88EECasdsadfgasdgadsa@207.46.248.16:

> I'm looking at my debugger right now and can clearly see that two
> threads have their execution pointer residing within the same Synclock
> block.
>
> One thread is executing a Monitor.Wait().  I know this suspends the
> thread but I didn't think this would open the gate for other threads
> to enter the block.
>
> Any ideas what could be happening?
>
> Joel Moore
>

And just to clarify some, here is a snippet of code from the MSDN sample
that I derived my own code from.  It's the Serial LCD sample:

lock (responseSignal)
{
    responsePacket = null;
    // comPort
    com.Write(packetXMitBuffer, 0, dataLength + 4);
    if (Monitor.Wait(responseSignal, MAX_RESPONSE_TIME))
    {
            return responsePacket;
    }
}

Elswhere there is a Receive() thread that Pulses the Monitor once the COM
port receives enough data.

After doing a little more reading it seems that the Monitor.Wait() does
release the lock which would imply that this sample code is faulty, no? 
Seems like I'd need to use a seperate object for the Monitor.Wait() call
than what was used to lock the code.
Author
7 Sep 2006 10:09 AM
Robinson
"Joel Moore" <asdads***@asdaadad.com> wrote in message
news:Xns9836906449125asdsadfgasdgadsa@207.46.248.16...
> Joel Moore <asdads***@asdaadad.com> wrote in
> news:Xns98368CE88EECasdsadfgasdgadsa@207.46.248.16:
>
>> I'm looking at my debugger right now and can clearly see that two
>> threads have their execution pointer residing within the same Synclock
>> block.
>>
>> One thread is executing a Monitor.Wait().  I know this suspends the
>> thread but I didn't think this would open the gate for other threads
>> to enter the block.
>>

Which object are synchronising on and are you sure you are synchronising on
the same instance for all threads?