473,785 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To lock or not?

I have a class as follows:

public class OperationFeedba ck
{
DateTime _startTime;

public DateTime StartTime
{
get
{
return _startTime;
}
set
{
_startTime = value;
}
}

}

Set will occur in a background thread. Getting will occur from my GUI
thread. Should I implement a lock inside the get and set? If I so why?

Thanks,

- Fritz
Jan 15 '08 #1
11 7987
Fritz,

If the StartTime property is the only one item that you need to
synchronize, then I would say in the class is fine. However, as a general
guideline, I think that there are few classes that should do this (even this
one). The reason for this is that operations are usually more complex than
simple get/sets, and you really should be wrapping those entire operations
in lock blocks to make sure that the whole operation is synchronized, not
just one part of it.

Of course, if you encapsulate your operations well enough, you can
synchronize access to those in a class.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<fr********@gma il.comwrote in message
news:13******** *************** ***********@i12 g2000prf.google groups.com...
>I have a class as follows:

public class OperationFeedba ck
{
DateTime _startTime;

public DateTime StartTime
{
get
{
return _startTime;
}
set
{
_startTime = value;
}
}

}

Set will occur in a background thread. Getting will occur from my GUI
thread. Should I implement a lock inside the get and set? If I so why?

Thanks,

- Fritz

Jan 15 '08 #2
If I leave the class as is? Is it possible that when the "get" is
called from one thread in the middle of the "set" from another, that
the value could contain part of the old and part of the new value
(since DateTime is not atomic). If I want to ensure a proper value
when I "get", I suspect I need to lock (this is what I am trying to
confirm / understand).

- Fritz
Jan 15 '08 #3
the value could contain part of the old and part of the new value

So are you saying that you are worried about having a stored value of say
"3/3/2003 3:33 PM" and then having the set method called to set a new value
of say "5/5/2005 5:55 AM" and during this process call the get method and
end up reading something like "5/5/2005 3:33 PM" (contain part of the old
and part of the new value)?

Did I totally misunderstand your question? :)
Jan 15 '08 #4
On Jan 15, 3:44 pm, "Rene" <a...@b.comwrot e:
the value could contain part of the old and part of the new value

So are you saying that you are worried about having a stored value of say
"3/3/2003 3:33 PM" and then having the set method called to set a new value
of say "5/5/2005 5:55 AM" and during this process call the get method and
end up reading something like "5/5/2005 3:33 PM" (contain part of the old
and part of the new value)?

Did I totally misunderstand your question? :)
No, that is my concern. Although I'm not sure it would manifest itself
exactly like that (this would depend on the internal implementation
DateTime).

- Fritz
Jan 15 '08 #5
On 15 Jan, 22:16, Fritz <fritzcw...@gma il.comwrote:
On Jan 15, 3:44 pm, "Rene" <a...@b.comwrot e:
the value could contain part of the old and part of the new value
So are you saying that you are worried about having a stored value of say
"3/3/2003 3:33 PM" and then having the set method called to set a new value
of say "5/5/2005 5:55 AM" and during this process call the get method and
end up reading something like "5/5/2005 3:33 PM" (contain part of the old
and part of the new value)?
Did I totally misunderstand your question? :)

No, that is my concern. Although I'm not sure it would manifest itself
exactly like that (this would depend on the internal implementation
DateTime).

- Fritz
DateTime is a 64 bit structure. If you look at the MSDN entry there is
an explicit warning that it is not thread safe on certain
architectures as threads could switch before it is completely commited.
Jan 16 '08 #6

"Fritz" <fr********@gma il.comwrote in message
news:dc******** *************** ***********@y5g 2000hsf.googleg roups.com...
On Jan 15, 3:44 pm, "Rene" <a...@b.comwrot e:
the value could contain part of the old and part of the new value

So are you saying that you are worried about having a stored value of say
"3/3/2003 3:33 PM" and then having the set method called to set a new
value
of say "5/5/2005 5:55 AM" and during this process call the get method and
end up reading something like "5/5/2005 3:33 PM" (contain part of the old
and part of the new value)?

Did I totally misunderstand your question? :)

No, that is my concern. Although I'm not sure it would manifest itself
exactly like that (this would depend on the internal implementation
DateTime).

- Fritz
Yes, it looks like that could potentially happen (on 32-bit Windows). The
DateTime type stores its value internally as a UInt64. According to
http://msdn2.microsoft.com/en-us/library/ms684122.aspx, read and writes of
64 bit values are not guaranteed to be atomic on 32-bit Windows, so
potentially the writing thread might only get time to write half of the
bytes before it gets interupted by the reading thread.

I have no idea how common this problem is or if it even exists in practice.
The article only make a general statement regarding 32-bit Windows. The
behavoiur might very well be that it's an atomic operation on XP and Vista,
but not on 2000 for example (note: I'm only guessing here).

For the record, I've never seen it happen (fwiw :-)).

/claes


Jan 16 '08 #7
Doing it on that granular a level actually increases your chances of
deadlock, assuming you are mixing it with other methods/properties that
lock on that granular a level.
Why is that though? I would have locking get/set of each property would be
better as long as the property is doing nothing but returning some state.

Also, in this case I would guess a reader/writer lock would work fine as
well.

---
Ajay

Jan 18 '08 #8
On Thu, 17 Jan 2008 19:31:19 -0800, Ajay Kalra <aj*******@yaho o.comwrote:
> Doing it on that granular a level actually increases your chances of
deadlock, assuming you are mixing it with other methods/properties that
lock on that granular a level.

Why is that though? I would have locking get/set of each property would
be better as long as the property is doing nothing but returning some
state.
Consider first that deadlocking code is buggy code. So the real question
is, why would locking at a finer level of granularity lead to more bugs?

The answer is simply that of opportunity. If you have only one lock,
there's no chance for deadlock at all. If you have two locks, there's
some chance for deadlock but it's not too hard to design around that
possibility. If you have fifty locks, then you've got some huge number of
permutations that could lead to deadlock. Make a single mistake and BOOM!

Pete
Jan 18 '08 #9

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Thu, 17 Jan 2008 19:31:19 -0800, Ajay Kalra <aj*******@yaho o.com>
wrote:
>> Doing it on that granular a level actually increases your chances of
deadlock, assuming you are mixing it with other methods/properties that
lock on that granular a level.

Why is that though? I would have locking get/set of each property would
be better as long as the property is doing nothing but returning some
state.

Consider first that deadlocking code is buggy code. So the real question
is, why would locking at a finer level of granularity lead to more bugs?

The answer is simply that of opportunity. If you have only one lock,
there's no chance for deadlock at all. If you have two locks, there's
some chance for deadlock but it's not too hard to design around that
possibility. If you have fifty locks, then you've got some huge number of
permutations that could lead to deadlock. Make a single mistake and BOOM!

I didnt say that we have multiple lock objects. You can use the same object.
In this case, all getter can acquire a readonlylock. For setters, I would
use the same object on which you apply the lock. Why is that hmm... bad?

I am curious to know because we follow this pattern and have had no issues
at all. I would like to know the downside that we are missing.
---
Ajay

Jan 18 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9534
by: xixi | last post by:
can someone explain to me what is internal p lock, internal s lock, internal v lock? when i have IS lock or IX lock , i always have these internal locks together for the application handle Application handle = 261 Application ID = AC100482.GD3A.00F85FE51898 Sequence number = 0006 Application name = db2jccmain Authorization ID ...
0
3389
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in lock-wait, waiting to acquire row-level X locks. The lock-waits are behind applications that have row-level X locks on different rows (honestly). Both executing and lock-waiting applications have been granted IX table locks.
7
703
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a
3
2688
by: Invalid | last post by:
I launch a worker thread that periodically reads a volatile bool abortRequested, which could be set to true by my main form. IOW, there is one thread that can read that bool and one different thread that can set it. See code below for an overview. In a C or C++ environment, my current approach would be adequate (no need to acquire exclusive access to abortRequested). I'm guessing that I'm still OK under C# because bool is a value type....
0
17817
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico Mariani, performance architect for the Microsoft® .NET runtime and longtime Microsoft developer, mentioned to Dr. GUI in an e-mail conversation recently that a fairly common practice (and one that's, unfortunately, described in some of our...
1
15671
by: charlies224 | last post by:
Hi, I am writting a software that requires me to make sure the Num Lock is always on and Caps Lock is always off. First, I know how to detect if Num Lock or Caps Lock is on or off (if someone is interested, let me know and I will send you the codes). Once we know if the stat of Num Lock/ Caps Lock is not what we desired, we just send the Num Lock / Caps Lock key to change the stat. From most of
3
6664
by: Raj | last post by:
I created a refresh deferred MQT, and during full refresh there were 4 or 5 lock waits, all waiting on a 'S' lock on Internal Catalog Cache ? Can some one explain how to prevent this from happening?
2
5867
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 ...... Deadlocked Statement: Type : Dynamic Operation: Execute
190
8186
by: blangela | last post by:
If you had asked me 5 years ago about the future of C++, I would have told you that its future was assured for many years to come. Recently, I have been starting to wonder. I have been teaching C++ at a local polytechnical school here in Vancouver, Canada for approximately 8 years. Six years ago, at the height (or should I say volume?) of the internet bubble, I had 80+ students per semester in my C++ course. Now I am fortunate to have...
94
30354
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.