473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2767
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
3
by: boxim | last post by:
got some static props in some classes some of the props need to refer to other static members of the other classes however, cos they're not simple types, cant use const, have to use readonly how...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
74
by: Mark | last post by:
Hello Friends Please check the code below. One in C# and other in VB .Net In C# I am not able to access a static property by an instance variable, but in VB I can do it easily. The Error is ...
5
by: WebMatrix | last post by:
Hello, It might seem like a stupid question to some. But I need to put this issue to rest once and for all, since it keeps coming up in code reviews every now and then. There’s a static...
14
by: Shimon Sim | last post by:
I have a static variable defined something like this private static Dictionary<string, string> roles = new Dictionary<string,string>(); Can I safely assume that it will be live for the live of...
7
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.