473,398 Members | 2,404 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,398 software developers and data experts.

Only static (Shared) types are thread safe ?!

dee
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee
Nov 19 '05 #1
11 2217
There is overhead in making a method thread safe. Microsoft typically
will not add the overhead to an instance (non static) method, because
90% of the time the overhead will be unneeded. This leaves it up to
the developer to add thread safety only when they need it.

Microsoft will typically make static methods threadsafe, because there
is a good chance the Type will be used in an MT environment.

I have a couple articles on the topic, let me know if they help or if
they can be improved:
Statics And Thread Safety (Parts I and II)
http://odetocode.com/Articles/313.aspx
http://odetocode.com/Articles/314.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 31 Aug 2005 08:55:09 -0400, "dee" <de*@home.net> wrote:
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee


Nov 19 '05 #2
dee wrote:
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee


I think this is a language problem here. I believe the phrase:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations"

is meant to be read

"Any public static (Shared in Visual Basic) memebers that this type
exposes are safe for multithreaded operations"

rather than

"Any public static (Shared in Visual Basic) members defined to be of
this type are safe for multithreaded operations"

It's quite ambiguous phrasing, but I believe that what I have stated
(the second quote) is the intended meaning.

Does this clear things up?

Damien

Nov 19 '05 #3
dee
Thanks Scott nice articles.
I guess my problem is with the meaning of the line from help file:
"Any public static (Shared in Visual Basic) members of this type are safe
for
multithreaded operations. Any instance members are not guaranteed to be
thread safe."

When they day "member" then mean either a method or a property?
"instance member" could be a method that uses a static proptery? If so, then
they should be handled like other static methods since thier user does
doesnt know how they are implemented.

Thanks for your time.
Dee
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:jm********************************@4ax.com...
There is overhead in making a method thread safe. Microsoft typically
will not add the overhead to an instance (non static) method, because
90% of the time the overhead will be unneeded. This leaves it up to
the developer to add thread safety only when they need it.

Microsoft will typically make static methods threadsafe, because there
is a good chance the Type will be used in an MT environment.

I have a couple articles on the topic, let me know if they help or if
they can be improved:
Statics And Thread Safety (Parts I and II)
http://odetocode.com/Articles/313.aspx
http://odetocode.com/Articles/314.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 31 Aug 2005 08:55:09 -0400, "dee" <de*@home.net> wrote:
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their
own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is
that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee

Nov 19 '05 #4
dee
Yes Damian Thanks.
In addition, why would an intrinsic method be unsafe? cuz it might access
static data? Then why not thread safe those as well?
"Damien" <Da*******************@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
dee wrote:
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their
own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is
that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee


I think this is a language problem here. I believe the phrase:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations"

is meant to be read

"Any public static (Shared in Visual Basic) memebers that this type
exposes are safe for multithreaded operations"

rather than

"Any public static (Shared in Visual Basic) members defined to be of
this type are safe for multithreaded operations"

It's quite ambiguous phrasing, but I believe that what I have stated
(the second quote) is the intended meaning.

Does this clear things up?

Damien

Nov 19 '05 #5
dee
OleDbCommand class doesnt have any static members anyway
what is the point of mentioning static members are thread safe ??

"dee" <de*@home.net> wrote in message
news:OW**************@TK2MSFTNGP12.phx.gbl...
Yes Damian Thanks.
In addition, why would an intrinsic method be unsafe? cuz it might access
static data? Then why not thread safe those as well?
"Damien" <Da*******************@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
dee wrote:
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their
own
stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is
that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee


I think this is a language problem here. I believe the phrase:

"Any public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations"

is meant to be read

"Any public static (Shared in Visual Basic) memebers that this type
exposes are safe for multithreaded operations"

rather than

"Any public static (Shared in Visual Basic) members defined to be of
this type are safe for multithreaded operations"

It's quite ambiguous phrasing, but I believe that what I have stated
(the second quote) is the intended meaning.

Does this clear things up?

Damien


Nov 19 '05 #6
when the docs refer to members thread safety, they are refering to
accessing that property from different threads.

when they say statics are thread safe, but instance are not, they mean, that
the static can be access from any thread safefully (as locking as been
implementd), but instance members can only be safely accessed from the
creating thread.

if an non-thread safe instance member is to be accessed from multiple
threads, you must add code to support this.

-- bruce (sqlwork.com)
"dee" <de*@home.net> wrote in message
news:uH**************@TK2MSFTNGP15.phx.gbl...
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 multithreaded operations. Any instance members are not guaranteed to
be thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their
own stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is
that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee

Nov 19 '05 #7
dee
Thanks Bruce

"Bruce Barker" <br******************@safeco.com> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
when the docs refer to members thread safety, they are refering to
accessing that property from different threads.

when they say statics are thread safe, but instance are not, they mean,
that the static can be access from any thread safefully (as locking as
been implementd), but instance members can only be safely accessed from
the creating thread.

if an non-thread safe instance member is to be accessed from multiple
threads, you must add code to support this.

-- bruce (sqlwork.com)
"dee" <de*@home.net> wrote in message
news:uH**************@TK2MSFTNGP15.phx.gbl...
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 multithreaded operations. Any instance members are not guaranteed to
be thread safe."
I have 2 questions:

1. I thought dynamic variables are thread-safe since threads have their
own stack and static data had to be synchronized. However, many many .NET
classes say ONLY static members of thier type are thread safe. Why is
that?
2. Do we still have to synchronize access to this shared data?

3. If only static members are thread-safe why all the examples use non
static data?

Thanks.
Dee


Nov 19 '05 #8
dee wrote:
OleDbCommand class doesnt have any static members anyway
what is the point of mentioning static members are thread safe ??

It's just a boilerplate statement that they've liberally sprinkled
throughout the documentation. I wouldn't try to read too much into it.

In the general case (when you're not passing instances around between
threads), it's only the static members that you would be concerned
about w.r.t thread safety, since those are the only ones which multiple
threads might conceivably call (on the same object, in this case the
type). Once you start passing instances between threads is when you
start having to think about implementing mutual exclusion (unless the
documentation, in that case, states that all members are thread safe).

HTH,

Damien

Nov 19 '05 #9
On Wed, 31 Aug 2005 11:10:15 -0400, "dee" <de*@home.net> wrote:

When they day "member" then mean either a method or a property?
"instance member" could be a method that uses a static proptery? If so, then
they should be handled like other static methods since thier user does
doesnt know how they are implemented.

Thanks for your time.
Dee


Yes, members would include both properties and methods.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #10
dee
Thanks Damien

"Damien" <Da*******************@hotmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
dee wrote:
OleDbCommand class doesnt have any static members anyway
what is the point of mentioning static members are thread safe ??

It's just a boilerplate statement that they've liberally sprinkled
throughout the documentation. I wouldn't try to read too much into it.

In the general case (when you're not passing instances around between
threads), it's only the static members that you would be concerned
about w.r.t thread safety, since those are the only ones which multiple
threads might conceivably call (on the same object, in this case the
type). Once you start passing instances between threads is when you
start having to think about implementing mutual exclusion (unless the
documentation, in that case, states that all members are thread safe).

HTH,

Damien

Nov 19 '05 #11
dee
Thanks Allen.

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:lq********************************@4ax.com...
On Wed, 31 Aug 2005 11:10:15 -0400, "dee" <de*@home.net> wrote:

When they day "member" then mean either a method or a property?
"instance member" could be a method that uses a static proptery? If so,
then
they should be handled like other static methods since thier user does
doesnt know how they are implemented.

Thanks for your time.
Dee


Yes, members would include both properties and methods.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #12

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...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
2
by: S | last post by:
Hi there, I'm building an application that makes heavy use of centralized static methods (and variables) and would just appreciate hearing someone grade my understanding of the concept. In...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
12
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.