473,668 Members | 2,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What exactly does it mean when they say

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?
Jul 5 '06 #1
18 1970
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.nosp amwrote in message
news:uN******** ******@TK2MSFTN GP02.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?

Jul 5 '06 #2
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:
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.nosp amwrote in message
news:uN******** ******@TK2MSFTN GP02.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?

Jul 5 '06 #3
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]
ja******@online .microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.
Jul 5 '06 #4

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 ")

Jul 5 '06 #5
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.nosp amwrote in message
news:eP******** ********@TK2MSF TNGP04.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.nosp amwrote in message
news:uN******* *******@TK2MSFT NGP02.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?
Jul 5 '06 #6
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:
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 ")
Jul 5 '06 #7
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.nosp amwrote in message
news:un******** ******@TK2MSFTN GP02.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("4 2")

Jul 5 '06 #8
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:
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.nosp amwrote in message
news:un******** ******@TK2MSFTN GP02.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")

Jul 5 '06 #9
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.nosp amwrote in message
news:%2******** ********@TK2MSF TNGP03.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.nosp amwrote in message
news:un******* *******@TK2MSFT NGP02.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
understan d
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")
Jul 5 '06 #10

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

Similar topics

220
19001
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
112
10311
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please, share your experience in using IDENTITY as PK .
125
14708
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
86
7755
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: http://www.clagnut.com/blog/348/ I hope that's going to be a good discussion! Michael
2
3079
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the current user) that tells me when the user last read items in the aggregated database.
121
10029
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
24
3799
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
1
8735
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 are thread safe. Any instance members are not guaranteed to be thread safe." What exactly does this mean? Does this mean that if I call a shared method from 2 different threads, nothing whacky will happen? Also when it says that instance members...
13
5036
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
10
2102
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset statements into comments (placed an ' in front)
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.