Lock on an object and change its field. Does it work? - c#

If I write a class like this:
public class LockBool
{
public bool validity = true;
}
And then I lock on such an object and change its validity field:
LockBool lockObj = GetTheLockObject();
lock(lockObj)
{
//release some resources
lockObj.validity = false;
}
So if another thread locks on the same object, it can read the validity for flow control.
lock(lockObj)
{
if(!validity)
return;
//do something here
}
Does it work as I expect, without throwing exception?
(If it works but it's bad practice, I also would like to know the details. Thanks!)
P.S.: The reason I tried to use the type with a bool field is to create variable number of lock objects. And it seems difficult to prevent other threads from acquiring the lock after I release resources. So a bool field can tell a thread if this lock is still valid, or it should be garbage-collected.

Please read this article.
using this kind of locking is discouraged, usually you should define a simple object varibale like
private static readonly object _lock;
with this kind of lock object you can sync between instances of your class (because it is static and shared between all instances) and no one can change its value (because it is readonly) and no one can use a lock on it from outside of class and make things go wrong (because it is private)
and if you want to just make the object thread safe (not all objects that are instantiated) you can remove the static and just use as many private object as you should.
UPDATE base on your comment
you can define a class like this
public class LockWrapper<T>
{
public T InsideClass { get; set; }
public readonly object _lock;
}
then use this when you need multiple instance of some class and want to lock on them separately

Related

Need to synchronize two classes

I have two classes A & B. Both are calling each other and having their own locks. I am getting a deadlock in one particular scenario. Here is the sample code.
class A : Interface1, Interface2
{
private B _bInstance = new B();
private object _aSync = new object();
private static A Instance;
private A(){}
public GetInstance()
{
if (Instance == null) Instance = new A();
return Instance;
}
void Method1()
{
lock(_aSync)
{
_bInstance.Method1();
}
}
void WriteData()
{
lock (_aSync)
{
WriteToFile();
}
}
}
class B
{
private object _bSync = new object();
void Method1()
{
lock (_bSync)
{
// Have some code here which need to protect my
// member variables.
A.GetInstance.WriteData();
}
}
void OneSecondTimerEvent()
{
lock (_bSync)
{
// Have some code here which need to protect my
// member variables.
A.GetInstance.WriteData();
}
}
}
How do I synchronize the OneSecondTimerEvent(), if One second timer gets triggered When the A.Method1() is being executed?
Yes, your code shows canonical example of deadlock - 2 resources waiting for each other to continue.
To resolve you can:
manually order lock statements (i.e. B never takes additional locks if A already have lock),
scope locks to only internal state of each class and never nest locks. In this case sometimes you'd need to copy state to call external methods.
use other synchronization primitives/constructs that allow such nesting (i.e. Reader-Writer locks).
Rather than try and solve this particular deadlock issue (which btw, is a classic result of locking things in an inconsistent order), I would strongly advise designing a better relationship between A and B. The fact you had to use a static instance to achieve a circular dependency should be a big clue you've done something wrong. Perhaps A and B should reference a 3rd class C, which is solely responsible for locking and writing the data? (Although it's difficult to say without a bit more context).

Can I call Monitor.Pulse from a different class in C#

In an application that I am developing I will be using 2 threads to do various operations. (I will not go into detail here.) These threads work in loops, checking if there is work to be done, doing work, calculating the time they need to wait and waiting. (See below)
public Global : System.Web.HttpApplication
{
private static Thread StartingDateThread;
private static Thread DeadlineDateThread;
private static object o1;
private static object o2;
public static Thread GetStartingDateThreadInstance
{
get
{
if(StartingDateThread==null)
{
StartingDateThread=new Thread(new ThreadStart(MonitorStartingDates));
}
return StartingDateThread;
}
}
public static Thread GetDeadlineThreadInstance
{
get
{
if(DeadlineDateThread==null)
{
DeadlineDateThread=new Thread(new ThreadStart(MonitorDeadlines));
}
return DeadlineDateThread;
}
}
public static object GetFirstObjectInstance
{
get
{
if(o1==null)
{
o1=new object();
}
return o1;
}
}
public static object GetSecondObjectInstance
{
get
{
if(o2==null)
{
o2=new object();
}
return o2;
}
}
protected void Application_Start(object sender, EventArgs e)
{
GetStartingDateThreadInstance.Start();
GetDeadlineThreadInstance.Start();
//////////////////////
////Do other stuff.
}
public void MonitorStartingDates()
{
while(true)
{
//Check if there is stuff to do.
//Do stuff if available.
//Check if there will be stuff to do in the future and if there is, check
//the time to wake up.
//If there is nothing to do, sleep for a pre-determined 12 hours.
if(StuffToDoInFuture)
{
Monitor.Enter(GetFirstObjectInstance);
Monitor.Wait(WaitingTime);
Monitor.Exit(GetFirstObjectInstance);
}
else
{
Monitor.Enter(GetFirstObjectInstance);
Monitor.Wait(new TimeSpan(12, 0, 0));
Monitor.Exit(GetFirstObjectInstance);
}
}
}
public void MonitorDeadlines()
{
while(true)
{
//Check if there is stuff to do.
//Do stuff if available.
//Check if there will be stuff to do in the future and if there is, check
//the time to wake up.
//If there is nothing to do, sleep for a pre-determined 3 days and 12 hours.
if(StuffToDoInFuture)
{
Monitor.Enter(GetSecondObjectInstance);
Monitor.Wait(WaitingTime);
Monitor.Exit(GetSecondObjectInstance);
}
else
{
Monitor.Enter(GetSecondObjectInstance);
Monitor.Wait(new TimeSpan(3, 12, 0, 0));
Monitor.Exit(GetSecondObjectInstance);
}
}
}
As you can see these two threads are started in the Application_Start method in the asax file. They operate if there is stuff available to do and then they calculate the time period they need to wait and then they wait. However, as users of the web application do operations new records will be inserted into the database and there will be circumstances where any of the two threads will have to resume operation sooner than planned. So, say I have a method in my DataAccess class which inserts into the database new data. (See below)
public class DataAccess
{
///////////////
//
public void InsertNewAuction()
{
///Insert new row calculate the time
Monitor.Pulse(Global.GetFirstObjectInstance);
Monitor.Pulse(Global.GetSecondObjectInstance);
///
}
}
It seems like this is an invalid operation, because at the stage where the Monitor.Pulse is called from the InsertNewAuction method I get an exception. Something like "Object synchronization method was called from an unsynchronized block of code." Is there any way of doing this? Thanks for your help
As to the specific error you're seeing, this is because Monitor.Pulse must be called inside the Monitor lock, like this (I've used lock rather than Enter/Exit, as it's safer for making sure the lock is always released, since it uses a proper try/finally block):
lock (Global.GetFirstObjectInstance)
{
Monitor.Pulse(Global.GetFirstObjectInstance);
}
In regard to the more general design question here, it's often dangerous to expose lock objects as public (or even worse, global) fields. In particular, it can be a recipe for deadlocks when multiple global locks are exposed and acquired in differing orders or when you have cases like blocking dispatches to the UI thread while holding a lock. Consider looking into alternate ways to accomplish what you're after.
As noted in the other answer, you have to acquire the lock before you can call Monitor.Pulse() on the monitor object.
That said, your code has at least one other serious bug: you are not initializing the synchronization object in a thread-safe way, which could easily lead to two different threads using two different object instances, resulting in no synchronization between those threads:
public static object GetFirstObjectInstance
{
get
{
if(o1==null)
{
o1=new object();
}
return o1;
}
}
If two threads call this getter simultaneously, they each may see o1 as null and try to initialize it. Then each might return a different value for the object instance.
You should simply initialize the object in a initializer:
private static readonly object o1 = new object();
And then return it from the getter:
public static object GetFirstObjectInstance { get { return o1; } }
That addresses the thread-safety issue. But you still have other issues with the code. First, you should encapsulate synchronization in an object, not expose the actual synchronization object instance. Second, assuming you are going to expose the synchronization object, I don't understand why you bother with the property, since you made the field public. The field should be private if you want to use a property as well.
It would also be better if the property followed normal .NET naming conventions. A method that returned the object would have "Get" in the name, but a property would not. Just name it "FirstObjectInstance".
Also as noted by Dan, use lock everywhere you want to acquire the lock.
There may be other issues in the code as well...I didn't do a thorough review. But the above you need to fix for sure.

calling a static parameter from one public class to another

I'm trying to write a multi-threaded app , and I need to use Monitoer.Enter/Exit/Wait/Pulse
I've created a Lock object and used it in its own class like that
public partial class domain
/*I dont sure this is the good practice way to mange DDD Layers (if anybody have a comment about it)*/
{
Public class Peer2PeerCom
{
public static readonly object locker = new object();
//other stuff here
//...
//somwhere here
Monitor.Pulse(locker);
}
}
in the other class I want/need to use the locker like that
public class Control
{
public domain.Peer2PeerCom Dom_P2PCom = new domain.Peer2PeerCom();
internal void connection ( int port , string IpAdress)
{
Monitor.Enter(Dom_P2PCom.locker);
//do stuff here
Monitor.wait(Dom_P2PCom.locker);
//..
Monitor.Exit(Dom_P2PCom.locker);
}
}
But when I try I cannot recognize the locker , I think it is because it is static but I dont understand how to correct it without making the entire class static
You're trying to access a static member via a reference. That doesn't work in C#, fortunately - it can lead to very misleading code where it's allowed, e.g. in Java.
Instead, you should use the name of the class to access a static member:
lock (domain.Peer2PeerCom.locker)
{
...
// This should probably be in a while loop...
Monitor.Wait(domain.Peer2PeerCom.locker);
}
(I've used lock rather than explicitly calling Monitor.Enter and Monitor.Exit - it's more idiomatic, and easier to get right - in the code you've given, any exception in the code after entering the monitor would have cause the monitor to be "leaked" - you wouldn't have exited it.)
In general, I'd strongly recommend against using a public static field for a shared lock like this. It makes it much harder to reason about what's using the lock than if it's private within a class. (I'd recommend against public fields in general, along with underscores in class names, classes which are nested for no particular reason, and a class name of domain, too...)

Synchronization over objects in a dictionary

I have a dictionary that I maintain in C# which has a string to a class object mapping.
public class parent
{
public Dictionary<string, valueclass> clientToFileSystemMap {get;set;}
}
class valueclass
{
//some internal state
valueclass createclone()
{
create clone of this object and return
}
void update()
{
change state
}
}
Now there can be simultaneous threads which can be updating and cloning the same object at the same time. I want to synchronize the access so that clone does not return a half updated object.
One way that I found was create a private lock object in the class valueclass and acquire that lock before that operation. The other option would be to use [MethodImpl(MethodImplOptions.Synchronized)] as mentioned here - C# version of java's synchronized keyword?.
Another option could be to create a dictionary of objects similar to class objects and take lock on them.
What would be best way to do this?
You can use a ConcurrentDictionary to control access to the dictionary elements - but if sounds like you will need a private lock per object - as once a thread has a reference - the dictionary isn't really the issue.
private object privateLock = new object();
void update(){
lock(privateLock)
{
//change state
}
}
valueclass createclone(){
lock(privateLock)
{
//Clone
}
}
You can also use the SyncRoot provided by ICollection interface
http://msdn.microsoft.com/en-us/library/bb348148(v=vs.110).aspx

Lock code section in c#

My question may sound like many others here but it has a flavor I didn't find.
I am trying to understand the following logic
A generic object
public class GenericClass
{
public static void DoSomething(Object lockObj)
{
lock(lockObj)
{
// do something here
}
}
}
Class A
internal class A
{
private static _aLock = new Object();
public void Do_A_Thing()
{
GenericClass.DoSomething(_aLock);
}
}
Class B
internal class B
{
private static _bLock = new Object();
public void Do_B_Thing()
{
GenericClass.DoSomething(_bLock);
}
}
I just hope to confirm if my explanation is correct:
If multiple threads of class "A" will attempt simultaneously access code in "genericClass" method "DoSomething", this method will be locked to all but one instance of class "A". But a single instance of class "B" will be able to proceed with execution any time. If class "B" will also have multiple instances execute, they will not interfere with class "A" locks.
Is this correct based on what you see above?
Yes, your description sounds correct. It is perhaps a little unusual to pass the lock object in, but it'll work fine. The only change I would suggest is to make the static fields readonly so you can't accidentally change the value to a different object reference.
Your conclusion is correct but it is not a good practice to pass locked object around. I suggest to put the lock inside class A and B respectively.
I suggest to write:
internal class A
{
private static readonly _aLock = new Object();
public void Do_A_Thing()
{
lock (_aLock)
{
GenericClass.DoSomething();
}
}
}
Do you have a specific reason to put the lock in another class? Maybe you can solve your problem in a different way?
Also keep in mind that in some conditions, maybe it is not your case, you can have a deadlock if class A and B call each other (A->B->A).
Yes, that is correct. The locks in A and the locks in B are completely unaware of each other. The code will only be blocked when there is another thread locking it with the same object as identifier.
If you are using generics, then something like
public class MyGadget<T>
{
static readonly SyncRoot = new object() ;
public T SynchronizedMethod()
{
lock ( SyncRoot )
{
SynchronizedMethodGuts() ;
}
}
}
should do what you want because MyGadget<Foo> and MyGadget<Bar> are different classes: they each have their own, different SyncRoot field.

Categories