Home All Groups Group Topic Archive Search About

What exactly does it mean when they say

Author
5 Jul 2006 2:53 PM
cj
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?

Author
5 Jul 2006 2:58 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:uNN4pKEoGHA.2028@TK2MSFTNGP02.phx.gbl...
> 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?
Author
5 Jul 2006 4:01 PM
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?

Marina Levit [MVP] wrote:
Show quoteHide quote
> 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" <cj@nospam.nospam> wrote in message
> news:uNN4pKEoGHA.2028@TK2MSFTNGP02.phx.gbl...
>> 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?
>
>
Author
5 Jul 2006 4:46 PM
Jared Parsons [MSFT]
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]
jared***@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Author
5 Jul 2006 4:51 PM
Travers Naran
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")
Author
5 Jul 2006 5:58 PM
cj
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:
Show quoteHide quote
> 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")
>
Author
5 Jul 2006 6:03 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
> 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")
>>
Author
5 Jul 2006 6:23 PM
cj
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:
Show quoteHide quote
> 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" <cj@nospam.nospam> wrote in message
> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>> 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")
>>>
>
>
Author
5 Jul 2006 6:26 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
> 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" <cj@nospam.nospam> wrote in message
>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>> 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")
>>>>
>>
Author
5 Jul 2006 6:45 PM
cj
Ok, I see now the S icon which says the synchronized method is the only
method in the queue class that is thread safe and that explains that
line about the synchronized wrapper.  So two things are running through
my head now.  1.  I guess my using synclock is a good way of making my
threadcount class threadsafe so many threads can use it w/o interfering
with each other.  Must be as it works.  2.  I'm curious about using a
shared method.  I keep reading that you have to instigate a class for it
to become an object and use it.  Clearly you don't but that confuses me
as to how to view it.  Is it an object w/o instigation?

Marina Levit [MVP] wrote:
Show quoteHide quote
> 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" <cj@nospam.nospam> wrote in message
> news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
>> 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" <cj@nospam.nospam> wrote in message
>>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>>> 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")
>>>>>
>
Author
5 Jul 2006 6:55 PM
Marina Levit [MVP]
Sorry, I don't know what 'instigation' is.  Let's say you have a class named
MyClass with a shared method called MySharedMethod.

In your code, you can just call MyClass.MySharedMethod().  That is all. You
don't need to do anything else.

There is no object created here - there is no instance of MyClass.
MySharedMethod belongs to the type, and not to any particular instance of
that type.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:up6S4MGoGHA.3504@TK2MSFTNGP02.phx.gbl...
> Ok, I see now the S icon which says the synchronized method is the only
> method in the queue class that is thread safe and that explains that line
> about the synchronized wrapper.  So two things are running through my head
> now.  1.  I guess my using synclock is a good way of making my threadcount
> class threadsafe so many threads can use it w/o interfering with each
> other.  Must be as it works.  2.  I'm curious about using a shared method.
> I keep reading that you have to instigate a class for it to become an
> object and use it.  Clearly you don't but that confuses me as to how to
> view it.  Is it an object w/o instigation?
>
> Marina Levit [MVP] wrote:
>> 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" <cj@nospam.nospam> wrote in message
>> news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
>>> 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" <cj@nospam.nospam> wrote in message
>>>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>>>> 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")
>>>>>>
>>
Author
5 Jul 2006 7:13 PM
cj
My fault it's Instantiation not Instigation which frankly now that I've
looked up both in the dictionary Instantiation is to create while
Instigation means to start.  Still seem quite the same.  No wonder I
always thought it was Instigation.


Marina Levit [MVP] wrote:
Show quoteHide quote
> Sorry, I don't know what 'instigation' is.  Let's say you have a class named
> MyClass with a shared method called MySharedMethod.
>
> In your code, you can just call MyClass.MySharedMethod().  That is all. You
> don't need to do anything else.
>
> There is no object created here - there is no instance of MyClass.
> MySharedMethod belongs to the type, and not to any particular instance of
> that type.
>
> "cj" <cj@nospam.nospam> wrote in message
> news:up6S4MGoGHA.3504@TK2MSFTNGP02.phx.gbl...
>> Ok, I see now the S icon which says the synchronized method is the only
>> method in the queue class that is thread safe and that explains that line
>> about the synchronized wrapper.  So two things are running through my head
>> now.  1.  I guess my using synclock is a good way of making my threadcount
>> class threadsafe so many threads can use it w/o interfering with each
>> other.  Must be as it works.  2.  I'm curious about using a shared method.
>> I keep reading that you have to instigate a class for it to become an
>> object and use it.  Clearly you don't but that confuses me as to how to
>> view it.  Is it an object w/o instigation?
>>
>> Marina Levit [MVP] wrote:
>>> 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" <cj@nospam.nospam> wrote in message
>>> news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
>>>> 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" <cj@nospam.nospam> wrote in message
>>>>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>>>>> 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")
>>>>>>>
>
>
Author
6 Jul 2006 6:44 AM
Jeffrey Tan[MSFT]
Hi Cj,

Do you understand the difference between Shared methods and normal object
methods? This is a key concept in OOP area. Normally, Shared methods
belongs to the class itself, while normal object methods belongs to certain
instance of the class. So invoking Shared methods needs only the class
name, not initializing an object instance, while invoking an object method,
you have to create an instance of the class first and use this object
instance reference to invoke the method.

From "Queue Class" in MSDN, we can read that:
"Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe."

So Queue.Synchronized(this is the only shared/static member of Queue class)
method can be invoked by multithreads without any problem, while other
members of "Queue Class" should be protected with synclock.

Hope this is clear.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
6 Jul 2006 7:11 AM
Cor Ligthert [MVP]
cj,

Your application has this patron in memory when you are running

Main module part 'created by the mainform or by yourself
Modules or Shared classes what both is the same

Somewhere temporary in memory as long as needed managed by your managed code
    Instanced objects

Very confusing is that some people call often an object a Class and typical
C# people call a module as well a class (static class although there is in
my idea not much static anymore about it).

A class is nothing more than a template from a certain type that can be
instanced.

When I did read yesterday the message from Marina, I had the idea that she
had made a kind of typo telling that a shared class did belong to a type,
while it is to the main (stack) modules or in C# language main static class.

I had however the idea that you did understand it, therefore I did not place
any message.

I hope this makes it a little bit clearer.

Cor


Show quoteHide quote
"cj" <cj@nospam.nospam> schreef in bericht
news:%23T$UVcGoGHA.1332@TK2MSFTNGP05.phx.gbl...
> My fault it's Instantiation not Instigation which frankly now that I've
> looked up both in the dictionary Instantiation is to create while
> Instigation means to start.  Still seem quite the same.  No wonder I
> always thought it was Instigation.
>
>
> Marina Levit [MVP] wrote:
>> Sorry, I don't know what 'instigation' is.  Let's say you have a class
>> named MyClass with a shared method called MySharedMethod.
>>
>> In your code, you can just call MyClass.MySharedMethod().  That is all.
>> You don't need to do anything else.
>>
>> There is no object created here - there is no instance of MyClass.
>> MySharedMethod belongs to the type, and not to any particular instance of
>> that type.
>>
>> "cj" <cj@nospam.nospam> wrote in message
>> news:up6S4MGoGHA.3504@TK2MSFTNGP02.phx.gbl...
>>> Ok, I see now the S icon which says the synchronized method is the only
>>> method in the queue class that is thread safe and that explains that
>>> line about the synchronized wrapper.  So two things are running through
>>> my head now.  1.  I guess my using synclock is a good way of making my
>>> threadcount class threadsafe so many threads can use it w/o interfering
>>> with each other.  Must be as it works.  2.  I'm curious about using a
>>> shared method. I keep reading that you have to instigate a class for it
>>> to become an object and use it.  Clearly you don't but that confuses me
>>> as to how to view it.  Is it an object w/o instigation?
>>>
>>> Marina Levit [MVP] wrote:
>>>> 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" <cj@nospam.nospam> wrote in message
>>>> news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
>>>>> 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" <cj@nospam.nospam> wrote in message
>>>>>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>>>>>> 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")
>>>>>>>>
>>
Author
6 Jul 2006 12:22 PM
cj
Jeffrey and Cor,  I keep hearing and even read in Cor's response "A
class is nothing more than a template from a certain type that can be
instanced." yet if it's a shared method in a class the class doesn't
have to be instanced.  I'm trying to think in the cookie cutter as a
class and each cookie as an instance of it.  Yet it appears the cookie
cutter has shared methods and can be used without making a cookie.  What
would be an example of this?

Then in this code I'm currently using I never make an instance of this
class yet it functions.  I know it's defined as Public Class.  So why do
I keep hearing a class is a model like a cookie cutter to make instances
of (cookies) and then in practice it appears many times a class doesn't
need an instance made of it at all?

And what if a class had a shared method and instances are made of it?
How could this work?  Would all instances change when the shared method
is used?  Can you give me a cookie cutter example of that?

     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




Cor Ligthert [MVP] wrote:
Show quoteHide quote
> cj,
>
> Your application has this patron in memory when you are running
>
> Main module part 'created by the mainform or by yourself
> Modules or Shared classes what both is the same
>
> Somewhere temporary in memory as long as needed managed by your managed code
>     Instanced objects
>
> Very confusing is that some people call often an object a Class and typical
> C# people call a module as well a class (static class although there is in
> my idea not much static anymore about it).
>
> A class is nothing more than a template from a certain type that can be
> instanced.
>
> When I did read yesterday the message from Marina, I had the idea that she
> had made a kind of typo telling that a shared class did belong to a type,
> while it is to the main (stack) modules or in C# language main static class.
>
> I had however the idea that you did understand it, therefore I did not place
> any message.
>
> I hope this makes it a little bit clearer.
>
> Cor
>
>
> "cj" <cj@nospam.nospam> schreef in bericht
> news:%23T$UVcGoGHA.1332@TK2MSFTNGP05.phx.gbl...
>> My fault it's Instantiation not Instigation which frankly now that I've
>> looked up both in the dictionary Instantiation is to create while
>> Instigation means to start.  Still seem quite the same.  No wonder I
>> always thought it was Instigation.
>>
>>
>> Marina Levit [MVP] wrote:
>>> Sorry, I don't know what 'instigation' is.  Let's say you have a class
>>> named MyClass with a shared method called MySharedMethod.
>>>
>>> In your code, you can just call MyClass.MySharedMethod().  That is all.
>>> You don't need to do anything else.
>>>
>>> There is no object created here - there is no instance of MyClass.
>>> MySharedMethod belongs to the type, and not to any particular instance of
>>> that type.
>>>
>>> "cj" <cj@nospam.nospam> wrote in message
>>> news:up6S4MGoGHA.3504@TK2MSFTNGP02.phx.gbl...
>>>> Ok, I see now the S icon which says the synchronized method is the only
>>>> method in the queue class that is thread safe and that explains that
>>>> line about the synchronized wrapper.  So two things are running through
>>>> my head now.  1.  I guess my using synclock is a good way of making my
>>>> threadcount class threadsafe so many threads can use it w/o interfering
>>>> with each other.  Must be as it works.  2.  I'm curious about using a
>>>> shared method. I keep reading that you have to instigate a class for it
>>>> to become an object and use it.  Clearly you don't but that confuses me
>>>> as to how to view it.  Is it an object w/o instigation?
>>>>
>>>> Marina Levit [MVP] wrote:
>>>>> 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" <cj@nospam.nospam> wrote in message
>>>>> news:%23a5DYAGoGHA.4124@TK2MSFTNGP03.phx.gbl...
>>>>>> 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" <cj@nospam.nospam> wrote in message
>>>>>>> news:un7aZyFoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>>>>>>> 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")
>>>>>>>>>
>
Author
6 Jul 2006 12:43 PM
Cor Ligthert [MVP]
cj,

I tried to keep it as short as possible however the names that are used and
are just from history are very confusing.
..
A Shared Class is nothing more as a module you could have written it as well
in VB as this.

Public Module MyThreadCount
         Private As New Object
         Private m_threadcount As Int32 = 0
         Public Sub Sub Increment()
             SyncLock (m_lock)
                 m_threadcount += 1
             End SyncLock
         End Sub

etc.

A shared class is called in past a static class. In mine opinion is the name
completely false.

It is a class that is direct used as a module, it has no constructor (Sub
New).

I hope this gives an idea?

Cor
Author
6 Jul 2006 1:02 PM
cj
Yes, that helps.  Just another case of confusing names and terms.

I'm still wondering can a class have shared methods and have instances
of it?


Cor Ligthert [MVP] wrote:
Show quoteHide quote
> cj,
>
> I tried to keep it as short as possible however the names that are used and
> are just from history are very confusing.
> .
> A Shared Class is nothing more as a module you could have written it as well
> in VB as this.
>
> Public Module MyThreadCount
>          Private As New Object
>          Private m_threadcount As Int32 = 0
>          Public Sub Sub Increment()
>              SyncLock (m_lock)
>                  m_threadcount += 1
>              End SyncLock
>          End Sub
>
> etc.
>
> A shared class is called in past a static class. In mine opinion is the name
> completely false.
>
> It is a class that is direct used as a module, it has no constructor (Sub
> New).
>
> I hope this gives an idea?
>
> Cor
>
>
Author
6 Jul 2006 11:04 PM
Travers Naran
cj wrote:
> Yes, that helps.  Just another case of confusing names and terms.
>
> I'm still wondering can a class have shared methods and have instances
> of it?

Yes.  When you declare a Class, think of it creating a single object of
that type that will be the template for all others.  It's an object in
and of itself.  Clear your mind of thread-safe because it's not
relevant to Shared vs. Instance at this moment.

Public Class Foobar

  Public Shared Shared_N as Integer
  Public Instance_N as Integer

  Public Shared Sub FooShared()
    Shared_N = Shared_N + 1
  End Sub

  Public Sub BarInstance()
    Instance_N = Instance_N + 1
  End Sub

  Public Sub Hybrid()
    Instance_N = Instance_N + 1
    Shared_N = Shared_N + 1 ' An instance can access shared variables
  End Sub
End Class

I can then do the following:

  Foobar.FooShared()
  System.Console.WriteLine(Foobar.Shared_N)

But what if I create an instance?  Try this:

  Dim f as New Foobar

  f.FooShared()
  System.Console.WriteLine(f.Shared_N)
  System.Console.WriteLine(Foobar.Shared_N) '' <== EXACTLY the same
memory space as f.Shared_N

  f.FooShared() '' <== EXACTLY the same method is called as
Foobar.FooShared()

A shared member (field or method) can be called straight on the class
object.  It can also be called or referenced by instance methods.
Author
5 Jul 2006 4:58 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:eP%239YxEoGHA.4784@TK2MSFTNGP04.phx.gbl...
> 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" <cj@nospam.nospam> wrote in message
>> news:uNN4pKEoGHA.2028@TK2MSFTNGP02.phx.gbl...
>>> 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?
>>