473,498 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

postponed Initialization in multithreaded enviroment.

I am trying to implement postponed initialization (object is not initialize
till requested).

public class clsStore
{
volatile List<clsPictureGroup_lstPictureGroups = null;

public List<clsPictureGroupPictureGroups
{
get
{
if (_lstPictureGroups == null)
{
lock (this)
{
if (_lstPictureGroups == null)
_lstPictureGroups = LoadPictureGroup();
}
}
return _lstPictureGroups;
}
}
}

Obviously this code suppose to run in multithreaded enviroment in ASP.NET.
Anyone sees any problem in this code?
As far as i can see there is no 'race' condition here and code is safe.
Thanks
George.

Oct 27 '08 #1
9 1170
"George" <no*****@comcast.netwrote in message
news:u7**************@TK2MSFTNGP06.phx.gbl...
I am trying to implement postponed initialization (object is not
initialize till requested).

public class clsStore
{
volatile List<clsPictureGroup_lstPictureGroups = null;

public List<clsPictureGroupPictureGroups
{
get
{
if (_lstPictureGroups == null)
{
lock (this)
{
if (_lstPictureGroups == null)
_lstPictureGroups = LoadPictureGroup();
}
}
return _lstPictureGroups;
}
}
}

Obviously this code suppose to run in multithreaded enviroment in ASP.NET.
Anyone sees any problem in this code?
As far as i can see there is no 'race' condition here and code is safe.
There are a few problems.

1) Best practice is to create a separate member to act as the lock variable:
private object _locker = new object()
2) The member need not be volatile - you are only going to change it while
it is locked, right?
3) You do not need to initialize it to null, as that is the default value.
This is not C or C++ - there is a specific default value for every type, so
there is no need to initialize something just to make sure you're not
accessing uninitialized data. There _is_ no uninitialized data.

The fourth is just a matter of style. Classes are not an unusual construct
in C#. In fact, they are the norm. It is really very redundant to prefix
every class with "cls". Similarly, do you really want to prefix all of your
lists with "lst"? You will learn more from hovering the mouse over the
member in the editor than by seeing "lst", which could mean any sort of list
(unless you restrict "lst" to be only List<T>).

--
John Saunders | MVP - Connected System Developer

Oct 27 '08 #2
its not really threadsafe. you have a race condition on (_lstPictureGroups ==
null), as this is not an atomic operation (not threadsafe), nor is the
assignment. the hole is small and you would probably get away with it.

-- bruce (sqlwork.com)
"George" wrote:
I am trying to implement postponed initialization (object is not initialize
till requested).

public class clsStore
{
volatile List<clsPictureGroup_lstPictureGroups = null;

public List<clsPictureGroupPictureGroups
{
get
{
if (_lstPictureGroups == null)
{
lock (this)
{
if (_lstPictureGroups == null)
_lstPictureGroups = LoadPictureGroup();
}
}
return _lstPictureGroups;
}
}
}

Obviously this code suppose to run in multithreaded enviroment in ASP.NET.
Anyone sees any problem in this code?
As far as i can see there is no 'race' condition here and code is safe.
Thanks
George.

Oct 27 '08 #3
"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
its not really threadsafe. you have a race condition on (_lstPictureGroups
==
null), as this is not an atomic operation (not threadsafe), nor is the
assignment. the hole is small and you would probably get away with it.
Bruce, it is thread-safe, since he checks the value both before and after
the lock.
--
John Saunders | MVP - Connected System Developer

Oct 27 '08 #4
See inline...
"John Saunders" <no@dont.do.that.comwrote in message
news:uo**************@TK2MSFTNGP04.phx.gbl...
"George" <no*****@comcast.netwrote in message
news:u7**************@TK2MSFTNGP06.phx.gbl...
>I am trying to implement postponed initialization (object is not
initialize till requested).

public class clsStore
{
volatile List<clsPictureGroup_lstPictureGroups = null;

public List<clsPictureGroupPictureGroups
{
get
{
if (_lstPictureGroups == null)
{
lock (this)
{
if (_lstPictureGroups == null)
_lstPictureGroups = LoadPictureGroup();
}
}
return _lstPictureGroups;
}
}
}

Obviously this code suppose to run in multithreaded enviroment in
ASP.NET.
Anyone sees any problem in this code?
As far as i can see there is no 'race' condition here and code is safe.

There are a few problems.

1) Best practice is to create a separate member to act as the lock
variable: private object _locker = new object()
I know it's best but only if you are locking (in my case clsStore) for any
other reason. If not, then i do not see any difference between lock(this) or
lock(_locker).
2) The member need not be volatile - you are only going to change it while
it is locked, right?
I think it needs to be.. I am afraid the compiler will optimize line #1 and
line #2. Is not what volatile for.... to prevent that kind of optimization.

1: if (_lstPictureGroups == null)
{
lock (this)
{
2: if (_lstPictureGroups == null)
_lstPictureGroups = LoadPictureGroup();

3) You do not need to initialize it to null, as that is the default value.
This is not C or C++ - there is a specific default value for every type,
so there is no need to initialize something just to make sure you're not
accessing uninitialized data. There _is_ no uninitialized data.
It's just my C++ experince playing here.... Sorry, I just feel bad if i do
not initalize variable specifically :)
>
The fourth is just a matter of style. Classes are not an unusual construct
in C#. In fact, they are the norm. It is really very redundant to prefix
every class with "cls". Similarly, do you really want to prefix all of
your lists with "lst"? You will learn more from hovering the mouse over
the member in the editor than by seeing "lst", which could mean any sort
of list (unless you restrict "lst" to be only List<T>).

Yea, I kind of used to that style... I like Hungarian notation. All my code
has lst...txt..chk...map
May be I am just an old school but I find it easier.
Thanks for your comments...
George.
Oct 27 '08 #5
I think I am ok with that line
if (_lstPictureGroups == null)

My only concern is if it allows dirty read.... Meaning that if
_lstPictureGroups has not been fully set and basically there is a point in
time when _lstPictureGroups returns some garbage.

What do you guys think? Valid concern?

Thanks
George.
"John Saunders" <no@dont.do.that.comwrote in message
news:en**************@TK2MSFTNGP02.phx.gbl...
"bruce barker" <br*********@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
>its not really threadsafe. you have a race condition on
(_lstPictureGroups ==
null), as this is not an atomic operation (not threadsafe), nor is the
assignment. the hole is small and you would probably get away with it.

Bruce, it is thread-safe, since he checks the value both before and after
the lock.
--
John Saunders | MVP - Connected System Developer
Oct 27 '08 #6
"George" <no*****@comcast.netwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
I think I am ok with that line
if (_lstPictureGroups == null)

My only concern is if it allows dirty read.... Meaning that if
_lstPictureGroups has not been fully set and basically there is a point in
time when _lstPictureGroups returns some garbage.

What do you guys think? Valid concern?
It's only set under lock, so I don't see that it's possible.
--
John Saunders | MVP - Connected System Developer

Oct 27 '08 #7
It's set under lock.
But read happens without any regard of "lock"
if (_lstPictureGroups == null) is done without lock attempt....
George.

"John Saunders" <no@dont.do.that.comwrote in message
news:ud**************@TK2MSFTNGP02.phx.gbl...
"George" <no*****@comcast.netwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
>I think I am ok with that line
if (_lstPictureGroups == null)

My only concern is if it allows dirty read.... Meaning that if
_lstPictureGroups has not been fully set and basically there is a point
in time when _lstPictureGroups returns some garbage.

What do you guys think? Valid concern?

It's only set under lock, so I don't see that it's possible.
--
John Saunders | MVP - Connected System Developer
Oct 27 '08 #8
"George" <no*****@comcast.netwrote in message
news:#G**************@TK2MSFTNGP02.phx.gbl...
It's set under lock.
But read happens without any regard of "lock"
if (_lstPictureGroups == null) is done without lock attempt....
I'm very sure that checking a reference for null is an atomic operation in
..NET.

Also, note that he properly checks the reference twice - once before, and
once after taking out the lock. It is possible for multiple threads to see
that the field is null and attempt to acquire the lock. Only one of those
threads will acquire the lock and then find that the field is still null,
and set it to a non-null value. The other threads will acquire the lock, in
turn, and find that the field has already been set to a non-null value. They
will therefore not set it to a different non-null value.

Like the old saying, "measure twice, cut once", but this one is, "check
twice, set once".

--
John Saunders | MVP - Connected System Developer

Oct 27 '08 #9
>>I'm very sure that checking a reference for null is an atomic operation in
>>.NET.
I am inclined to that too.
Looks like my code is clear...:)
Thanks a lot for your comments guys..
George.
"John Saunders" <no@dont.do.that.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"George" <no*****@comcast.netwrote in message
news:#G**************@TK2MSFTNGP02.phx.gbl...
>It's set under lock.
But read happens without any regard of "lock"
if (_lstPictureGroups == null) is done without lock attempt....

I'm very sure that checking a reference for null is an atomic operation in
.NET.

Also, note that he properly checks the reference twice - once before, and
once after taking out the lock. It is possible for multiple threads to see
that the field is null and attempt to acquire the lock. Only one of those
threads will acquire the lock and then find that the field is still null,
and set it to a non-null value. The other threads will acquire the lock,
in turn, and find that the field has already been set to a non-null value.
They will therefore not set it to a different non-null value.

Like the old saying, "measure twice, cut once", but this one is, "check
twice, set once".

--
John Saunders | MVP - Connected System Developer
Oct 27 '08 #10

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

Similar topics

2
6616
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
3
1997
by: clqrq | last post by:
i have just a little question: guess i have a class with a static function and i have different threads running. do i have to expect the problem that 2 treads try to acces CA::static() at the...
15
1383
by: Dilip | last post by:
The subject is a bit misleading because I really can't figure out what the following code snippet is doing and would appreciate any help in deciphering it. I mean I can understand code-wise what...
3
6312
by: Ronen Yacov | last post by:
Hi everybody, When declaring a singleton, we write: static CMySingle::instance() { static CMySingle instance; return instance; }
0
6998
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...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6884
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...
0
4586
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...
0
3090
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
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 ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.