Threadsafe property
-
Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?
class Manager
{
private object locker = new object();private MyType threadSafeProperty = new MyType(); public MyType ThreadSafeProperty { get { lock (locker) { return threadSafeProperty; } } set { lock (locker) { threadSafeProperty = value; } } }
}
Thank you, Phil
I won’t not use no double negatives.
-
Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?
class Manager
{
private object locker = new object();private MyType threadSafeProperty = new MyType(); public MyType ThreadSafeProperty { get { lock (locker) { return threadSafeProperty; } } set { lock (locker) { threadSafeProperty = value; } } }
}
Thank you, Phil
I won’t not use no double negatives.
AFAIK, "lock" is not recomended since it can result in permanent lockups of an object for ever. Monitor.TryEnter does the same but takes an argument specifying timeout interval. Also, the locker object should be readonly.
-
Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?
class Manager
{
private object locker = new object();private MyType threadSafeProperty = new MyType(); public MyType ThreadSafeProperty { get { lock (locker) { return threadSafeProperty; } } set { lock (locker) { threadSafeProperty = value; } } }
}
Thank you, Phil
I won’t not use no double negatives.
This will not work if your intent is to synchronize access to the
MyType
instance. You are only holding the lock while getting the reference to the instance. You then return the instance to the calling code which may then use it without synchronization. The solution depends on what the calling code does with theMyType
instance. You could expose a synchronization object in theManager
class, but that is open to abuse. Or you could add methods for manipulating the instance through theManager
class and use locks in those methods. YMMV. Nick---------------------------------- Be excellent to each other :)
-
Hi everybody! just wanted to ask a short question: I have a property, that should be (externally) threadsafe. Is this good coding style, or do you have a better solution?
class Manager
{
private object locker = new object();private MyType threadSafeProperty = new MyType(); public MyType ThreadSafeProperty { get { lock (locker) { return threadSafeProperty; } } set { lock (locker) { threadSafeProperty = value; } } }
}
Thank you, Phil
I won’t not use no double negatives.
Philip. Your concept of locking an object while returning a reference to it as a means of synchronization indicates you are far from understanding the fundamental concepts of synchronization. I strongly urge you to study the subject more before attempting any implementation requiring thread synchronization.
-
This will not work if your intent is to synchronize access to the
MyType
instance. You are only holding the lock while getting the reference to the instance. You then return the instance to the calling code which may then use it without synchronization. The solution depends on what the calling code does with theMyType
instance. You could expose a synchronization object in theManager
class, but that is open to abuse. Or you could add methods for manipulating the instance through theManager
class and use locks in those methods. YMMV. Nick---------------------------------- Be excellent to each other :)
Thank you for you answer Nick. That was actually why asked this, I had a bad feeling about that ;) MyType is some kind of generic List. I wanted to ensure that I had exclusive access to the list as long as I was manipulating it. Stupid idea now ;) I will expose the lock and acquire it as long as I am manipulating the list. Phil
I won’t not use no double negatives.
-
Philip. Your concept of locking an object while returning a reference to it as a means of synchronization indicates you are far from understanding the fundamental concepts of synchronization. I strongly urge you to study the subject more before attempting any implementation requiring thread synchronization.
-
Thank you for you answer Nick. That was actually why asked this, I had a bad feeling about that ;) MyType is some kind of generic List. I wanted to ensure that I had exclusive access to the list as long as I was manipulating it. Stupid idea now ;) I will expose the lock and acquire it as long as I am manipulating the list. Phil
I won’t not use no double negatives.
No problem. You might like to read http://www.albahari.com/threading/[^] :) Nick
---------------------------------- Be excellent to each other :)
-
Philip F. wrote:
I'll do that.
Then you will do well. If it helps you, back when I started trying to understand mulit-threading I found Jeffery Richters book Advanced Windows[^] extremely helpful. Keep in mind it was the first edition so no guarantees the new one is the same.