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

Nicolas Krzywinski opensource at site7even.de
Sun Sep 18 12:21:55 EDT 2011


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));
>>    }
>>   }
>>  }
>> }
>>
>
>


-- 
www.site7even.de | www.nskcomputing.de


More information about the Monodevelop-list mailing list