473,324 Members | 1,678 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,324 software developers and data experts.

what does this mean

Any public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.?

can anyone illustrate with an example.


Jul 21 '05 #1
3 1894
Sure. This is a common quote out of the .NET documentation.

Many classes have public static members, which means that an instance of the
class does nothave to exist in order to use the member.
For example, String.Format is a method that is called by stating, literally,
String.format. You don't first create a string object, then call the format
member on the object.

However, instance members are members that cannot be called unless an
instance of the class exists. For example, String.Insert() will insert one
string at a location within another. You call it by creating an object of
type string (String MyString; ) and then using the instance variable
(MyString.Insert).

The statement "instance members are not guaranteed to be thread safe" means
that you could create two threads in your application. If, in both threads,
you refer to the same instance (MyString) and one of them calls .Insert
while the other is assigning a value to the object itself, the CLR does not
guarantee that you will get the same answer every time you do this. In
fact, you could get some pretty weird results.

If you have two threads in your application, and you want to have them share
an object, lock the object before you write to it or read from it. That
way, if both threads try to access it at the same time, one thread will stop
and wait for the other one to unlock the object.

Hope this helps,
--- Nick

"Frazer" <Ic***@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Any public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.?

can anyone illustrate with an example.

Jul 21 '05 #2
thanx for your reply
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:XcbDc.111662$eu.63280@attbi_s02...
Sure. This is a common quote out of the .NET documentation.

Many classes have public static members, which means that an instance of the class does nothave to exist in order to use the member.
For example, String.Format is a method that is called by stating, literally, String.format. You don't first create a string object, then call the format member on the object.

However, instance members are members that cannot be called unless an
instance of the class exists. For example, String.Insert() will insert one string at a location within another. You call it by creating an object of
type string (String MyString; ) and then using the instance variable
(MyString.Insert).

The statement "instance members are not guaranteed to be thread safe" means that you could create two threads in your application. If, in both threads, you refer to the same instance (MyString) and one of them calls .Insert
while the other is assigning a value to the object itself, the CLR does not guarantee that you will get the same answer every time you do this. In
fact, you could get some pretty weird results.

If you have two threads in your application, and you want to have them share an object, lock the object before you write to it or read from it. That
way, if both threads try to access it at the same time, one thread will stop and wait for the other one to unlock the object.

Hope this helps,
--- Nick

"Frazer" <Ic***@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.?

can anyone illustrate with an example.


Jul 21 '05 #3
Nick Malik <ni*******@hotmail.nospam.com> wrote:
Sure. This is a common quote out of the .NET documentation.

Many classes have public static members, which means that an instance of the
class does nothave to exist in order to use the member.
For example, String.Format is a method that is called by stating, literally,
String.format. You don't first create a string object, then call the format
member on the object.

However, instance members are members that cannot be called unless an
instance of the class exists. For example, String.Insert() will insert one
string at a location within another. You call it by creating an object of
type string (String MyString; )
That doesn't create an object - it just declares a variable. I know I'm
being picky, but I think in discussions like this it helps to be
precise.
and then using the instance variable (MyString.Insert).

The statement "instance members are not guaranteed to be thread safe" means
that you could create two threads in your application. If, in both threads,
you refer to the same instance (MyString) and one of them calls .Insert
while the other is assigning a value to the object itself, the CLR does not
guarantee that you will get the same answer every time you do this. In
fact, you could get some pretty weird results.
String is a bad example here, as strings *are* thread-safe due to being
immutable. StringBuilder would be a good example though.
If you have two threads in your application, and you want to have them share
an object, lock the object before you write to it or read from it. That
way, if both threads try to access it at the same time, one thread will stop
and wait for the other one to unlock the object.


Or preferrably (IMO) you lock on some other shared object, which only
exists for locking purposes.
See http://www.pobox.com/~skeet/csharp/m...ml#lock.choice

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
1
by: Frank Rizzo | last post by:
Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.