I
I.Benc
Hi!
I have trouble with the Monitor class.
I did some troubleshooting and finally figured out the
problem. The thing is, the Monitor class simple does not work
as specified.
Better said, if used by the framework through lock( obj ) statements
it works fine, however if used through Monitor.Enter( obj ) and
Monitor.Exit( obj), deadlocks can occur.
Here is a simple example that shows this behavior. If the example
is run as specified everything is fine. If the lock statements are
commented, and Enter & Exit statements uncommented deadlock occurs.
Why this happens? .NET framework help specifies that this should be
the same. I found in the help, that Monitor maintains Waiting and
Ready
queues of threads for each object, but I haven't found the clear
explanation
of the meanings of this queues.
Any sugestions?
using System;
using System.Threading;
namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
string s;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
Thread t1 = new Thread( new ThreadStart( c1.f1 ));
Thread t2 = new Thread( new ThreadStart( c1.f2 ));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.ReadLine();
}
public Class1()
{
s = "0";
}
void f1()
{
lock( s )
{
//Monitor.Enter( s );
broj = (Convert.ToInt32( s ) + 1).ToString();
Console.WriteLine( s );
//Monitor.Exit( s );
}
}
void f2()
{
lock( s )
{
//Monitor.Enter( s );
broj = (Convert.ToInt32( s ) + 1).ToString();
Console.WriteLine( s );
//Monitor.Exit( s );
}
}
}
}
I have trouble with the Monitor class.
I did some troubleshooting and finally figured out the
problem. The thing is, the Monitor class simple does not work
as specified.
Better said, if used by the framework through lock( obj ) statements
it works fine, however if used through Monitor.Enter( obj ) and
Monitor.Exit( obj), deadlocks can occur.
Here is a simple example that shows this behavior. If the example
is run as specified everything is fine. If the lock statements are
commented, and Enter & Exit statements uncommented deadlock occurs.
Why this happens? .NET framework help specifies that this should be
the same. I found in the help, that Monitor maintains Waiting and
Ready
queues of threads for each object, but I haven't found the clear
explanation
of the meanings of this queues.
Any sugestions?
using System;
using System.Threading;
namespace ConsoleApplication4
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
string s;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
Thread t1 = new Thread( new ThreadStart( c1.f1 ));
Thread t2 = new Thread( new ThreadStart( c1.f2 ));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.ReadLine();
}
public Class1()
{
s = "0";
}
void f1()
{
lock( s )
{
//Monitor.Enter( s );
broj = (Convert.ToInt32( s ) + 1).ToString();
Console.WriteLine( s );
//Monitor.Exit( s );
}
}
void f2()
{
lock( s )
{
//Monitor.Enter( s );
broj = (Convert.ToInt32( s ) + 1).ToString();
Console.WriteLine( s );
//Monitor.Exit( s );
}
}
}
}