Trying to understand [SynchronizationAttribute][1] reentry is required flag.
According to albahari:
If the Synchronization attribute is applied with the reentrant argument true:
[Synchronization(true)]then the synchronization context's lock will be temporarily released when execution leaves the context.
According to my understanding synchronization context is always released when program gets out of function owned by object with context code.
Sample below runs in [Synchronization(true)] case like there is no synchronization content at all while [Synchronization(false)] case blocks as expected and makes me happy by feeling synchronization context.
[Synchronization(true)]
public class DeadLock: ContextBoundObject
{
public DeadLock Other;
public void Demo() { Thread.Sleep(1000); Console.WriteLine("demo "+ Thread.CurrentThread.ManagedThreadId); ; Other.Demo(); }
public static void Hello() { Console.WriteLine("hello"); }
}
class Program
{
static void Main(string[] args)
{
DeadLock d1 = new DeadLock();
DeadLock d2 = new DeadLock();
d1.Other = d2;
d2.Other = d1;
new Thread(d1.Demo).Start();
Console.WriteLine("startng d2.Demo");
d2.Demo();
}
}
What is difference between Synchronization(true) and class that has no synchronization context at all?