D and parallel programming

Julio César Carrascal Urquijo jcesar at phreaker.net
Tue Aug 7 10:30:17 PDT 2007


Regan Heath wrote:
> Anyone know what the C# behaviour is in this case?

using System;

class Foo
{
	int i;
	public void bar()
	{
		lock (this)
		{
			i++;
		}
	}

	public static void Main()
	{
		Foo f = new Foo();
		lock (f)
		{
			f.bar();
		}
	}
}

It works but it is always recommended to lock on a private object 
because there are some corner cases where it will dead-lock. Something 
like the following is always preferred:

	readonly object syncRoot = new object();

	(...)
	lock (syncRoot)
	{
		(...)
	}



More information about the Digitalmars-d mailing list