SyncRoot , lock(this) and Syncronized Pattern
21/01/2010 Leave a comment
You have an object and you want to use one of function that object as threadsafe you can use lock(object) statement for syncronization. But it can cause a deadlock if you are using “lock(this) ” . it occurs when derived classes want to try lock before your inner function execute and also it never gonna execute , because of your inner function wating first lock to be removed but it never be done here is the DeadLock!! .
public class MyClass
{
public void DoSomething()
{ //DO NOT USE locking like this
lock (this)
{
}
}
}
Dead Lock Example:
MyClass cs = new MyClass();
public void ThreadWorker() //this function is called by another thread
{
lock (cs) // DEADLOCK !!!
{
cs.DoSomething();
}
}
Here is the golden rule :
If you have an internal data structure that you want to prevent simultaneous access to by multiple threads, you should always make sure the object you’re locking on is not public.
The reasoning behind this is that a public object can be locked by anyone, and thus you can create deadlocks because you’re not in total control of the locking pattern.
This means that locking on this
is not an option, since anyone can lock on that object. Likewise, you should not lock on something you expose to the outside world.
Which means that the best solution is to use an internal object, and thus the tip is to just use Object
.
Locking data structures is something you really need to have full control over, otherwise you risk setting up a scenario for deadlocking, which can be very problematic to handle.
Example
public class MyClass
{
// Use an inner object for locking.
object _SyncRoot = new object();
public void DoSomething()
{
lock (_SyncRoot)
{
int val = DateTime.Now.Second;
Console.WriteLine("1) Do Someting Called by " + Thread.CurrentThread.Name);
while (val - 1 != DateTime.Now.Second)
{
//do some thing
}
Console.WriteLine("1) out");
}
}
}
Most of syncronization tasks be used when working on collections and Syncronized Pattern and ICollection.SyncRoot Property is already exists for c# collections you dont need to create your own.
But if you have a custom object you can use Syncronized Pattern for your classes . There is an example of Thread Safe wrapper code:
class MyClass
{
private class SynchronizedClass : MyClass
{
private object syncRoot = new object();
private MyClass _instance;
public SynchronizedClass(MyClass cls) { _instance = cls; }
public override bool IsSynchronized { get { return true; } }
public override void Funk1() { lock (syncRoot) { _instance.Funk1(); } }
public override void Funk2() { lock (syncRoot) { _instance.Funk2(); } }
}
public virtual bool IsSynchronized { get { return false; } }
public static MyClass Synchronized(MyClass cls)
{
if (!cls.IsSynchronized)
return new SynchronizedClass(cls);
return cls;
}
public virtual void Funk1() { /* Your code here */ }
public virtual void Funk2() { /* Your code here */ }
}
See:
http://msdn.microsoft.com/en-us/library/system.collections.icollection.syncroot.aspx
http://stackoverflow.com/questions/728896/whats-the-use-of-the-syncroot-pattern
Happy Codding !!!