[MonoDevelop] Monitor.Wait return false immediately on Mono Runtime

mono mono at webkai.net
Mon Oct 17 00:57:45 EDT 2011


I find out one new things.
That code return false in debug run the same.
But it return true in normal run.


> Aah I see the point - sorry I forgot about your writing for the immediate
> output in mono.
>
> I would try to Monitor.Pulse(anotherobject_not_the_lockobject) and
> Monitor.Wait(anotherobject_not_the_lockobject) because it is not
> recommended to use a lock object within the lock area. Just give it a try.
>
> Am 18.09.2011, 19:23 Uhr, schrieb mono <mono at webkai.net>:
>
>> Thank you for reply.
>> I understood race condition.
>> This code is simplified to understand the problem easily.
>>
>> I want to know why Moniter.Wait return false before 10 second past.
>> Probably it is mono specific.
>> I will ask about this problem in mono community.
>>
>>
>>> Ah and what I forgot to mention:
>>> For Monitor.Wait(..) to make sense, _I think_ it should not be
>>> encapsulated by the lock object is is waiting for - because then it will
>>> never return true if the lock object was not pulsed PRIOR to entering
>>> the
>>> lock area.
>>>
>>> Am 18.09.2011, 18:07 Uhr, schrieb Nicolas Krzywinski
>>> <opensource at site7even.de>:
>>>
>>>> Hello,
>>>>
>>>> you have built a beautiful example of a racing condition.
>>>>
>>>> Let me explain what your code does in my opinion:
>>>>
>>>> 1. Spawn thread that executes Async()
>>>> 2. Ensleeps this thread for 3 seconds
>>>> => Now it is unsure how far the thread was able to execute: is it
>>>> already
>>>> sitting on the lock?
>>>>
>>>> 3. 3 seconds passed, thread is resumed and the Main method as well
>>>> => Now it depends on first: the thread already sitting on the lock? and
>>>> second: how quickly a thread can be resumed by the runtime?
>>>>
>>>> ==> Depending on those (multiple) speed conditions either the main
>>>> method
>>>> or the thread will reach the lock first!
>>>>
>>>> Your example shows us that .NET and Mono is implemented different in
>>>> that
>>>> case.
>>>>
>>>> Regards,
>>>> 7even
>>>>
>>>> Am 18.09.2011, 04:49 Uhr, schrieb mono <mono at webkai.net>:
>>>>
>>>>> Hello
>>>>>
>>>>> I am using Monodevelop2.6 on Windows Vista.
>>>>>
>>>>> I build a following code by .NET Runtime.
>>>>> Then, 'True' is displayed in 3 second later.
>>>>>
>>>>> Next, I build by mono2.10.5 runtime.
>>>>> And I execute it. But 'False' is displayed in console immediately.
>>>>>
>>>>> Please, Advice.
>>>>>
>>>>>
>>>>> using System;
>>>>> using System.Threading;
>>>>>
>>>>> namespace MonoThreadTest
>>>>> {
>>>>>  class MainClass
>>>>>  {
>>>>>   static object lockObj = new object();
>>>>>  public static void Main (string[] args)
>>>>>   {
>>>>>    new Thread(new ThreadStart(Async)).Start();
>>>>>   Thread.Sleep(3 * 1000);
>>>>>   lock(lockObj)
>>>>>    {
>>>>>     Monitor.PulseAll(lockObj);
>>>>>    }
>>>>>   Console.Read();
>>>>>   }
>>>>>  static void Async()
>>>>>   {
>>>>>    lock(lockObj)
>>>>>    {
>>>>>     Console.WriteLine(Monitor.Wait(lockObj, 10 * 1000));
>>>>>    }
>>>>>   }
>>>>>  }
>>>>> } 



More information about the Monodevelop-list mailing list