Bug 15062 - Monitor lock behavior appears to cause severe starvation
Summary: Monitor lock behavior appears to cause severe starvation
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: 3.2.x
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-09-30 01:52 UTC by Shay Rojansky
Modified: 2014-01-17 14:44 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description Shay Rojansky 2013-09-30 01:52:50 UTC
In the code below we have two threads contending over a lock. Once a thread acquires it it sleeps for 20ms and then releases it.

I realize that probably no specific guarantees are made regarding the behavior of this kind of contention. But in the mono case (3.2.3), unlike in MS.NET, once a thread first acquires the lock it seems to reacquire it forever, never letting the other thread. This seems like undesirable behavior that could adversely affect multithreaded apps...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Playground
{
    public class Class1
    {
        public static void Main(String[] args)
        {
            Locker = new object();
            Done = new CountdownEvent(2);

            Thread t1 = new Thread(MonitorTestThreadHandler);
            Thread t2 = new Thread(MonitorTestThreadHandler);

            t1.Start(1);
            t2.Start(2);

            Done.Wait();
        }

        static object Locker;
        static CountdownEvent Done;

        static void MonitorTestThreadHandler(object oContext)
        {
            DateTime s = DateTime.Now;

            while (DateTime.Now < s.AddSeconds(10))
            {
                lock (Locker)
                {
                    Thread.Sleep(20);
                    Console.Error.WriteLine("Thread {0} here", oContext);
                }
            }

            Done.Signal();
        }
    }
}
Comment 1 Rodrigo Kumpera 2014-01-17 14:44:17 UTC
Mono uses the underlying OS locking primitives, it's up to them to offer fairness.