By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

clarification on Shared class constructor

P: n/a
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a shared member MyCollection.Count. Since the class has not been initialized, the Constructor is invoked and initializes class with some data from the database. Before the first thread exits the constructor, a second request is made to the IIS which spawns a second thread to call MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2) waits till the first thread is finished before it enters MyCollection.Count, (3) gets access to MyCollection.Count and get a zero (or a number less than the actual count) because the collection is not fully initialized yet.

Public Class MyCollection
Private Shared mCollection as New Hashtable



Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub

End Class

Should I be doing something like the following:

Public Classs MyCollection
Private Shared mCollection as New Hashtable

Private Shared Lock As New System.Threading.ReaderWriterLock
Public Shared Function Count() as Integer

Lock.AcquireReaderLock(Timeout.Infinite)

Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New
Try

Lock.AcquireWriterLock(Timeout.Infinite)

'code to initialize a datatable

...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally

Lock.ReleaseWriterLock()
End Try

End Sub

End Class

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a 2nd
static constructor which has a reference back to the 1st class (cyclical
reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
From what I understand, in ASP.NET, each HTTP requests is serviced by a
separate thread. So if my code uses a static Class with shared members and
properties, I can manage concurrent access by using something like the
Monitor or ReaderWriterLock class. It is rather difficult to simulate
multiple threads in a debug environment so I am hoping someone can enlighten
me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a
shared member MyCollection.Count. Since the class has not been initialized,
the Constructor is invoked and initializes class with some data from the
database. Before the first thread exits the constructor, a second request is
made to the IIS which spawns a second thread to call MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2) waits
till the first thread is finished before it enters MyCollection.Count, (3)
gets access to MyCollection.Count and get a zero (or a number less than the
actual count) because the collection is not fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable

Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class

Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock

Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New

Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class
Nov 17 '05 #2

P: n/a
Thanks Karl:

The article really helped.

Paul Wu

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:Op**************@TK2MSFTNGP14.phx.gbl...
Paul:
Static constructors are thread-safe, no if or buts about it. That means,
from a static constructor point of view, you are assured that...the only
thing you need to worry about is if your static constructor relies on a
2nd static constructor which has a reference back to the 1st class
(cyclical reference)...otherwise your safe.

As for as your properties, they aren't thread-safe...unless the underlying
class itself is (some .net classes are thread safe).

You should take a look at:
http://odetocode.com/Articles/313.aspx
and
http://odetocode.com/Articles/314.aspx

As you hopefully know, locks come with a hefty performance price and you
should consider alternatives if possible...I haven't had the need to use
static fields too often...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Paul Wu" <wu**********@gmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
From what I understand, in ASP.NET, each HTTP requests is serviced by a
separate thread. So if my code uses a static Class with shared members and
properties, I can manage concurrent access by using something like the
Monitor or ReaderWriterLock class. It is rather difficult to simulate
multiple threads in a debug environment so I am hoping someone can
enlighten me by telling me what happens in the following scenario?

A request is made to the IIS which in turn launches a thread to access a
shared member MyCollection.Count. Since the class has not been
initialized, the Constructor is invoked and initializes class with some
data from the database. Before the first thread exits the constructor, a
second request is made to the IIS which spawns a second thread to call
MyCollection.Count.

Will the second thread: (1) tries to invoke the constructor again, (2)
waits till the first thread is finished before it enters
MyCollection.Count, (3) gets access to MyCollection.Count and get a zero
(or a number less than the actual count) because the collection is not
fully initialized yet.
Public Class MyCollection
Private Shared mCollection as New Hashtable

Public Shared Function Count() as Integer
Return mCollection.Count
End Function

Shared Sub New
'code to initialize a datatable
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
End Sub
End Class

Should I be doing something like the following:
Public Classs MyCollection
Private Shared mCollection as New Hashtable
Private Shared Lock As New System.Threading.ReaderWriterLock

Public Shared Function Count() as Integer
Lock.AcquireReaderLock(Timeout.Infinite)
Count = mCollection.Count
Lock.ReleaseReaderLock()
End Function

Shared Sub New

Try
Lock.AcquireWriterLock(Timeout.Infinite)
'code to initialize a datatable
...
For each record in datatable
Dim obj as New Employee(record.column("name"))
mCollection.Add(key, obj)
Next
Finally
Lock.ReleaseWriterLock()
End Try
End Sub
End Class

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.