what does this mean

F

Frazer

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.
 
N

Nick Malik

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
 
F

Frazer

thanx for your reply


Nick Malik said:
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
 
J

Jon Skeet [C# MVP]

Nick Malik said:
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/multithreading.html#lock.choice
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top