473,585 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread-safety and Singleton methods

Guys,

I have been cracking my head over this concept in .NET framework. I have
read many posts on this topic but not clear about this and hence I am posting
it again.

If you have designed your class based on singleton pattern where ONLY ONE
instance of class exists for the WHOLE APPLICATION DOMAIN....how can the
public methods in that class be thread-safe? I have read thru posts where
they say that singleton class methods are thread-safe...I request you to
justify this conclusion.

How about static methods in singleton class .... are they thread-safe?

Scott Allen informed in one of his posts that:
----
Parameters are passed to a method using a stack. Each thread maintains it's
own stack so that the parameters are not shared among threads.
----

How about reference type variables....ar e they also passed using a stack?
Reference-types address is stored in the stack and not their values.....so if
their addresses are being passed, how come they are thread-safe?
Nov 19 '05 #1
1 1233
This might be useful:

http://odetocode.com/Articles/314.aspx

Reference types, as you say, have their address passed on the stack but
their actual data remains in the heap. However, I don't see this really
being a problem:

Why wouldn't static methods be thread-safe? they apply the same as any
other static method...if you are using static fields (such as the internal
instance of the singleton class) then of course if you change it once it'll
change it everywhere...bu t if that's a concern, perhaps you aren't using a
singleton properly?

I'm quite confused by your confusion. A static method isn't impacted by the
presence of a singleton...a static function isn't called on an instance of
the class (even if you jsut have one). If the static function is messing
around with the internal static instance,well that' sjust plain silly:

public class SomeClass
private static SomeClass _instance = new SomeClass();
public static SomeClass Instance{
get { return _instance}
}

public static void ScrewMeOver(){
//if you mess with the _instance field in here, you better lock
access
}
}

perhaps you could provide an example which you think would cause problems?

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@disc ussions.microso ft.com> wrote in message
news:81******** *************** ***********@mic rosoft.com...
Guys,

I have been cracking my head over this concept in .NET framework. I have
read many posts on this topic but not clear about this and hence I am posting it again.

If you have designed your class based on singleton pattern where ONLY ONE
instance of class exists for the WHOLE APPLICATION DOMAIN....how can the
public methods in that class be thread-safe? I have read thru posts where
they say that singleton class methods are thread-safe...I request you to
justify this conclusion.

How about static methods in singleton class .... are they thread-safe?

Scott Allen informed in one of his posts that:
----
Parameters are passed to a method using a stack. Each thread maintains it's own stack so that the parameters are not shared among threads.
----

How about reference type variables....ar e they also passed using a stack?
Reference-types address is stored in the stack and not their values.....so if their addresses are being passed, how come they are thread-safe?

Nov 19 '05 #2

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

Similar topics

14
2160
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code block and ensuing output. Seems like the output should be all interleaved and of course it's not. Running Python 2.2 from ActiveState on Windows...
4
2882
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I know of WxPython but I don't know if I can create a WxPython window, use gl rendering code in it and then put widgets on top of that...
7
2697
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you...
4
5425
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the...
5
2818
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no references to it. But what about threads? If I start a new thread that only does 1+1, is it garbage collected after that? If so, do all threads get garbage...
16
3293
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I have a thread "_itTaskThread" running. The application is listening on a TCP port. It accepts 2 connection from a client. I simulate a crash on the...
9
3058
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException System.Threading.ThreadInterruptedException System.Threading.ThreadStateException to see if I could get more information about why the thread stops...
4
6211
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global Function */ template < class FunType, class ArgType> Thread makeThread(Funtype fun, ArgType arg)
3
35065
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like accessing the database and servicing queries, aren't slowed as much. John Nagle
34
2773
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
7908
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...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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...
1
7950
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...
0
8212
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...
1
5710
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...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
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.