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

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:

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



        static object Locker;
        static CountdownEvent Done;

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

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

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.