468,555 Members | 2,182 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,555 developers. It's quick & easy.

Static Readonly Properties and Thread Safety

for example i have static readonly SqlParameter and i want to clone them at
runtime. as clone operation will not write to SqlParameter object, just
reading, should i lock that object during read operations?
Nov 17 '05 #1
4 2477
Hi ,

Is urs a Multi threaded App with a number of worker threads spawned .
even in that case does your threads do some mofifictaion to the Sql
Parameters , then only u may think of locking , but by default static
variables are thread safe and they always have a synchronized access ,
so this is not required in ur case .

thanks ,

Mrinal

The Crow wrote:
for example i have static readonly SqlParameter and i want to clone them at
runtime. as clone operation will not write to SqlParameter object, just
reading, should i lock that object during read operations?

Nov 17 '05 #2
no i wont modify parameters, just read it.

can u show me a reference proving that static members are thread safe by
default?
Nov 17 '05 #3
Hi ,

I think , i am myself confused due to contrasting expalantions available
on net but the fact remains that by default coding norms a static
variable can't be thread safe , since it has common access to many
clients but many classes in .Net are by default designed to incorporate
the issue and they have implemented an internal locking using mutex .

you may wnat to have alook through :

-- suggests that static classes are thread safe .
http://dotnet.org.za/armand/archive/...6/10/2080.aspx

-- Articles on thread safety -- more detailed .
http://odetocode.com/Articles/313.aspx
http://odetocode.com/Articles/314.aspx -- Talks about internal
implementation of lock .

So , you need to go into a class definition and check out the thread
safety being claimed by MS .

Meanwhile what i think is only Reading is not a problem at all , you may
not do any kind of any locking as it doesn't changes ur variable value .

Moreover locking has it's own performance hits .

For any class if you are doing some modification in value for static
object and it's MS document doesn't claims thread safety , then better
go for ur own implementation , as you can't even isolate the case and
check , as at different times it will yield different results .

thanks ,

Mrinal

The Crow wrote:
no i wont modify parameters, just read it.

can u show me a reference proving that static members are thread safe by
default?

Nov 17 '05 #4
i think ms is implementing thread safety for all static members by default,
but its a internal coding standart of ms. even the class doesnt include any
public static field, ms is adding a notification to SDK document that any
static members are thread safe. its not a feature of CLR or any language. so
if u access static members concurrently, you should always consider the
thread safety. but in my case not.
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Steve - DND | last post: by
3 posts views Thread by boxim | last post: by
74 posts views Thread by Mark | last post: by
14 posts views Thread by Shimon Sim | last post: by
6 posts views Thread by Olumide | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.