PC Review


Reply
Thread Tools Rate Thread

What exactly does it mean when they say

 
 
cj
Guest
Posts: n/a
 
      5th Jul 2006
members of this type are safe for multithreaded operations. Instance
members are not guaranteed to be thread-safe.


I'm under the impression before you can use a class you have to make an
instance of it. So how can a class be threadsafe by itself but an
instance of it not be?

I guess I don't get what exactly being threadsafe means. Multiple
theads can use the same instance of a class?
 
Reply With Quote
 
 
 
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      5th Jul 2006
Those classes may have shared methods. In which case, those methods are
thread safe. So if multiple threads are calling the same shared method,
that is safe and no extra precautions need to be taken by the developer.

If 2 threads are pointing to the same instance of the class, then those
instance methods are not guaranteed to be thread safe, and it is up to the
developer to synchronize access.

"cj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> members of this type are safe for multithreaded operations. Instance
> members are not guaranteed to be thread-safe.
>
>
> I'm under the impression before you can use a class you have to make an
> instance of it. So how can a class be threadsafe by itself but an
> instance of it not be?
>
> I guess I don't get what exactly being threadsafe means. Multiple theads
> can use the same instance of a class?



 
Reply With Quote
 
 
 
 
cj
Guest
Posts: n/a
 
      5th Jul 2006
Sorry but to me these two situations sound the same. I don't understand
what the difference is. To use a class I have to create an instance of
it, right? Then I have multiple threads that will want to call methods
in that instance of the class. Not thread safe? Or thread safe?

Marina Levit [MVP] wrote:
> Those classes may have shared methods. In which case, those methods are
> thread safe. So if multiple threads are calling the same shared method,
> that is safe and no extra precautions need to be taken by the developer.
>
> If 2 threads are pointing to the same instance of the class, then those
> instance methods are not guaranteed to be thread safe, and it is up to the
> developer to synchronize access.
>
> "cj" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> members of this type are safe for multithreaded operations. Instance
>> members are not guaranteed to be thread-safe.
>>
>>
>> I'm under the impression before you can use a class you have to make an
>> instance of it. So how can a class be threadsafe by itself but an
>> instance of it not be?
>>
>> I guess I don't get what exactly being threadsafe means. Multiple theads
>> can use the same instance of a class?

>
>

 
Reply With Quote
 
Jared Parsons [MSFT]
Guest
Posts: n/a
 
      5th Jul 2006
Hello cj,

> Sorry but to me these two situations sound the same. I don't
> understand what the difference is. To use a class I have to create an
> instance of it, right? Then I have multiple threads that will want to
> call methods in that instance of the class. Not thread safe? Or
> thread safe?


That case is not thread safe. The case that is thread safe are when you
call the "shared" methods. These are not associated with an instance and
the documentation indicates they are safe for multi-threaded access.

--
Jared Parsons [MSFT]
(E-Mail Removed)
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.


 
Reply With Quote
 
Travers Naran
Guest
Posts: n/a
 
      5th Jul 2006

cj wrote:
> Sorry but to me these two situations sound the same. I don't understand
> what the difference is. To use a class I have to create an instance of
> it, right?


No.

There are two types of methods on classes. Instance methods and Shared
methods.

An instance method is declared:
Public Sub Foo()
....
End Sub

A shared method is declared:
Public Shared Sub Bar()
....
End Sub

If the class is called Foobar, then to call Foo:

dim o as new Foobar
o.Foo()

Notice I had to create an instance first. But to call Bar:

Foobar.Bar()

I don't have to create an instance because it is _shared_. I simply
use the name of the class. In C++-speak, it's a static method. In OOP
speak, a _class_ method.

Example in the framework:
Int32.Parse("42")

 
Reply With Quote
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      5th Jul 2006
No, you do not always need an instance of a class to use it.

I think you need to read up on what shared methods are.

"cj" <(E-Mail Removed)> wrote in message
news:eP%(E-Mail Removed)...
> Sorry but to me these two situations sound the same. I don't understand
> what the difference is. To use a class I have to create an instance of
> it, right? Then I have multiple threads that will want to call methods in
> that instance of the class. Not thread safe? Or thread safe?
>
> Marina Levit [MVP] wrote:
>> Those classes may have shared methods. In which case, those methods are
>> thread safe. So if multiple threads are calling the same shared method,
>> that is safe and no extra precautions need to be taken by the developer.
>>
>> If 2 threads are pointing to the same instance of the class, then those
>> instance methods are not guaranteed to be thread safe, and it is up to
>> the developer to synchronize access.
>>
>> "cj" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> members of this type are safe for multithreaded operations. Instance
>>> members are not guaranteed to be thread-safe.
>>>
>>>
>>> I'm under the impression before you can use a class you have to make an
>>> instance of it. So how can a class be threadsafe by itself but an
>>> instance of it not be?
>>>
>>> I guess I don't get what exactly being threadsafe means. Multiple
>>> theads can use the same instance of a class?

>>


 
Reply With Quote
 
cj
Guest
Posts: n/a
 
      5th Jul 2006
Ok.

But the act of declaring Public Shared alone doesn't make it threadsafe
does it? I have this class MyThreadCount (see below) which creates
public shared classes but I was told I had to use the synclock to keep
multiple threads from manipulating the count at the same time. Is this
true?

Public Class MyThreadCount

Private Shared m_lock As New Object
Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()
SyncLock (m_lock)
m_threadcount += 1
End SyncLock
End Sub

Public Shared Sub Decrement()
SyncLock (m_lock)
m_threadcount -= 1
End SyncLock
End Sub

Public Shared ReadOnly Property ThreadCount() As Int32
Get
Dim _count As Int32
SyncLock (m_lock)
_count = m_threadcount
End SyncLock
Return _count
End Get
End Property
End Class


Travers Naran wrote:
> cj wrote:
>> Sorry but to me these two situations sound the same. I don't understand
>> what the difference is. To use a class I have to create an instance of
>> it, right?

>
> No.
>
> There are two types of methods on classes. Instance methods and Shared
> methods.
>
> An instance method is declared:
> Public Sub Foo()
> ...
> End Sub
>
> A shared method is declared:
> Public Shared Sub Bar()
> ...
> End Sub
>
> If the class is called Foobar, then to call Foo:
>
> dim o as new Foobar
> o.Foo()
>
> Notice I had to create an instance first. But to call Bar:
>
> Foobar.Bar()
>
> I don't have to create an instance because it is _shared_. I simply
> use the name of the class. In C++-speak, it's a static method. In OOP
> speak, a _class_ method.
>
> Example in the framework:
> Int32.Parse("42")
>

 
Reply With Quote
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      5th Jul 2006
No, having a method declared as Shared does not automatically make it thread
safe.

But if there is a note in the documentation that says it is thread safe,
then developers of that method took precautions to make sure it is.

"cj" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ok.
>
> But the act of declaring Public Shared alone doesn't make it threadsafe
> does it? I have this class MyThreadCount (see below) which creates public
> shared classes but I was told I had to use the synclock to keep multiple
> threads from manipulating the count at the same time. Is this true?
>
> Public Class MyThreadCount
>
> Private Shared m_lock As New Object
> Private Shared m_threadcount As Int32 = 0
>
> Public Shared Sub Increment()
> SyncLock (m_lock)
> m_threadcount += 1
> End SyncLock
> End Sub
>
> Public Shared Sub Decrement()
> SyncLock (m_lock)
> m_threadcount -= 1
> End SyncLock
> End Sub
>
> Public Shared ReadOnly Property ThreadCount() As Int32
> Get
> Dim _count As Int32
> SyncLock (m_lock)
> _count = m_threadcount
> End SyncLock
> Return _count
> End Get
> End Property
> End Class
>
>
> Travers Naran wrote:
>> cj wrote:
>>> Sorry but to me these two situations sound the same. I don't understand
>>> what the difference is. To use a class I have to create an instance of
>>> it, right?

>>
>> No.
>>
>> There are two types of methods on classes. Instance methods and Shared
>> methods.
>>
>> An instance method is declared:
>> Public Sub Foo()
>> ...
>> End Sub
>>
>> A shared method is declared:
>> Public Shared Sub Bar()
>> ...
>> End Sub
>>
>> If the class is called Foobar, then to call Foo:
>>
>> dim o as new Foobar
>> o.Foo()
>>
>> Notice I had to create an instance first. But to call Bar:
>>
>> Foobar.Bar()
>>
>> I don't have to create an instance because it is _shared_. I simply
>> use the name of the class. In C++-speak, it's a static method. In OOP
>> speak, a _class_ method.
>>
>> Example in the framework:
>> Int32.Parse("42")
>>



 
Reply With Quote
 
cj
Guest
Posts: n/a
 
      5th Jul 2006
Ok, believe it or not were making progress. I think between my
threadcount class and Travers examples I'm getting an idea of how to
write a class that's threadsafe or not.

But for MS classes... Right now I'm looking at the Queue class and it
says, "Public static (Shared in Visual Basic) members of this type are
safe for multithreaded operations." How do I tell which of the methods
listed under Queue Members are public static? They list Public
Constructor, Public Properties, Public Methods, and protected Methods.
I see the thread safe message a lot and I would like to know what
exactly it's saying is and isn't threadsafe about the class or how I use
it. If there is a better MS class example than the Queue class feel
free to point it out.


Marina Levit [MVP] wrote:
> No, having a method declared as Shared does not automatically make it thread
> safe.
>
> But if there is a note in the documentation that says it is thread safe,
> then developers of that method took precautions to make sure it is.
>
> "cj" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Ok.
>>
>> But the act of declaring Public Shared alone doesn't make it threadsafe
>> does it? I have this class MyThreadCount (see below) which creates public
>> shared classes but I was told I had to use the synclock to keep multiple
>> threads from manipulating the count at the same time. Is this true?
>>
>> Public Class MyThreadCount
>>
>> Private Shared m_lock As New Object
>> Private Shared m_threadcount As Int32 = 0
>>
>> Public Shared Sub Increment()
>> SyncLock (m_lock)
>> m_threadcount += 1
>> End SyncLock
>> End Sub
>>
>> Public Shared Sub Decrement()
>> SyncLock (m_lock)
>> m_threadcount -= 1
>> End SyncLock
>> End Sub
>>
>> Public Shared ReadOnly Property ThreadCount() As Int32
>> Get
>> Dim _count As Int32
>> SyncLock (m_lock)
>> _count = m_threadcount
>> End SyncLock
>> Return _count
>> End Get
>> End Property
>> End Class
>>
>>
>> Travers Naran wrote:
>>> cj wrote:
>>>> Sorry but to me these two situations sound the same. I don't understand
>>>> what the difference is. To use a class I have to create an instance of
>>>> it, right?
>>> No.
>>>
>>> There are two types of methods on classes. Instance methods and Shared
>>> methods.
>>>
>>> An instance method is declared:
>>> Public Sub Foo()
>>> ...
>>> End Sub
>>>
>>> A shared method is declared:
>>> Public Shared Sub Bar()
>>> ...
>>> End Sub
>>>
>>> If the class is called Foobar, then to call Foo:
>>>
>>> dim o as new Foobar
>>> o.Foo()
>>>
>>> Notice I had to create an instance first. But to call Bar:
>>>
>>> Foobar.Bar()
>>>
>>> I don't have to create an instance because it is _shared_. I simply
>>> use the name of the class. In C++-speak, it's a static method. In OOP
>>> speak, a _class_ method.
>>>
>>> Example in the framework:
>>> Int32.Parse("42")
>>>

>
>

 
Reply With Quote
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      5th Jul 2006
There is usually an icon in the documentation to indicate that a method is
shared. When you look at the help for that method, it says it is shared
there as well.

A lot of these classes don't actually have any useful shared methods that
you would ever call. You would always end up creating an instance of the
class, at which point those methods on that object are not thread safe.

Some classes might actually have shared methods that you would want to call.
It all depends on the class.

"cj" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Ok, believe it or not were making progress. I think between my
> threadcount class and Travers examples I'm getting an idea of how to write
> a class that's threadsafe or not.
>
> But for MS classes... Right now I'm looking at the Queue class and it
> says, "Public static (Shared in Visual Basic) members of this type are
> safe for multithreaded operations." How do I tell which of the methods
> listed under Queue Members are public static? They list Public
> Constructor, Public Properties, Public Methods, and protected Methods. I
> see the thread safe message a lot and I would like to know what exactly
> it's saying is and isn't threadsafe about the class or how I use it. If
> there is a better MS class example than the Queue class feel free to point
> it out.
>
>
> Marina Levit [MVP] wrote:
>> No, having a method declared as Shared does not automatically make it
>> thread safe.
>>
>> But if there is a note in the documentation that says it is thread safe,
>> then developers of that method took precautions to make sure it is.
>>
>> "cj" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> Ok.
>>>
>>> But the act of declaring Public Shared alone doesn't make it threadsafe
>>> does it? I have this class MyThreadCount (see below) which creates
>>> public shared classes but I was told I had to use the synclock to keep
>>> multiple threads from manipulating the count at the same time. Is this
>>> true?
>>>
>>> Public Class MyThreadCount
>>>
>>> Private Shared m_lock As New Object
>>> Private Shared m_threadcount As Int32 = 0
>>>
>>> Public Shared Sub Increment()
>>> SyncLock (m_lock)
>>> m_threadcount += 1
>>> End SyncLock
>>> End Sub
>>>
>>> Public Shared Sub Decrement()
>>> SyncLock (m_lock)
>>> m_threadcount -= 1
>>> End SyncLock
>>> End Sub
>>>
>>> Public Shared ReadOnly Property ThreadCount() As Int32
>>> Get
>>> Dim _count As Int32
>>> SyncLock (m_lock)
>>> _count = m_threadcount
>>> End SyncLock
>>> Return _count
>>> End Get
>>> End Property
>>> End Class
>>>
>>>
>>> Travers Naran wrote:
>>>> cj wrote:
>>>>> Sorry but to me these two situations sound the same. I don't
>>>>> understand
>>>>> what the difference is. To use a class I have to create an instance
>>>>> of
>>>>> it, right?
>>>> No.
>>>>
>>>> There are two types of methods on classes. Instance methods and Shared
>>>> methods.
>>>>
>>>> An instance method is declared:
>>>> Public Sub Foo()
>>>> ...
>>>> End Sub
>>>>
>>>> A shared method is declared:
>>>> Public Shared Sub Bar()
>>>> ...
>>>> End Sub
>>>>
>>>> If the class is called Foobar, then to call Foo:
>>>>
>>>> dim o as new Foobar
>>>> o.Foo()
>>>>
>>>> Notice I had to create an instance first. But to call Bar:
>>>>
>>>> Foobar.Bar()
>>>>
>>>> I don't have to create an instance because it is _shared_. I simply
>>>> use the name of the class. In C++-speak, it's a static method. In OOP
>>>> speak, a _class_ method.
>>>>
>>>> Example in the framework:
>>>> Int32.Parse("42")
>>>>

>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
\KernelObjects\CritSecOutOfMemoryEvent Help?? What does this exactly mean? CK Windows XP Configuration 1 17th Oct 2004 04:59 PM
What exactly does "created" mean in windows file system Jamie Windows XP Basics 2 9th Jun 2004 12:19 AM
Task scheduler - what exactly does 'idle' mean? Mike Hartigan Windows XP Help 0 10th Apr 2004 02:13 AM
what EXACTLY does it mean to RIP onto a Cd or DVD Suga Windows XP General 2 8th Dec 2003 06:46 PM
purchased eTrust EZ antivirus 12 days ago - they say I must renew my subscription - Are they Nuts ? dave Anti-Virus 7 30th Oct 2003 05:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:15 PM.