PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
What exactly does it mean when they say
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
What exactly does it mean when they say
![]() |
What exactly does it mean when they say |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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? |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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? |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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? > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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] jaredpar@online.microsoft.com All opinions are my own. All content is provided "AS IS" with no warranties, and confers no rights. |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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") |
|
|
|
#6 |
|
Guest
Posts: n/a
|
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" <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? >> |
|
|
|
#7 |
|
Guest
Posts: n/a
|
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") > |
|
|
|
#8 |
|
Guest
Posts: n/a
|
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") >> |
|
|
|
#9 |
|
Guest
Posts: n/a
|
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") >>> > > |
|
|
|
#10 |
|
Guest
Posts: n/a
|
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") >>>> >> |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

