Hello All,
If I create a class based on Singleton design pattern, are the methods in
this class thread-safe?
Consider the following class:
Public class Singleton
{
public int Add(int a, int b)
{
}
}
If suppose I have a request A which calls the method with params as 1 and 2
and another request simultaneously calls the method with params as 3 and 4
Is there a possibility that the params are overrided as follows
--> Request A calls the method with params as 1 and 4
--> Request A calls the method with params as 3 and 2
Thanks for your suggestions!!! 6 1161
it depends on the code. if your class contains no shareded variables, and
calls no unmanged code, it will be threadsafe.
-- bruce (sqlwork.com)
"Diffident" <Di*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** ***********@mic rosoft.com...
| Hello All,
|
| If I create a class based on Singleton design pattern, are the methods in
| this class thread-safe?
|
| Consider the following class:
|
| Public class Singleton
| {
|
| public int Add(int a, int b)
| {
|
| }
|
| }
|
| If suppose I have a request A which calls the method with params as 1 and
2
| and another request simultaneously calls the method with params as 3 and 4
|
| Is there a possibility that the params are overrided as follows
|
| --> Request A calls the method with params as 1 and 4
| --> Request A calls the method with params as 3 and 2
|
| Thanks for your suggestions!!!
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.
See: http://odetocode.com/Articles/313.aspx http://odetocode.com/Articles/314.aspx
--
Scott http://www.OdeToCode.com/blogs/scott/ Hello All,
If I create a class based on Singleton design pattern, are the methods in this class thread-safe?
Consider the following class:
Public class Singleton { public int Add(int a, int b) { }
}
If suppose I have a request A which calls the method with params as 1 and 2 and another request simultaneously calls the method with params as 3 and 4
Is there a possibility that the params are overrided as follows
--> Request A calls the method with params as 1 and 4 --> Request A calls the method with params as 3 and 2
Thanks for your suggestions!!!
1) Shared variables
2) Unmanaged code
Are these the necessary and sufficient conditions for singleton class
methods to be thread-safe?
"bruce barker" wrote: it depends on the code. if your class contains no shareded variables, and calls no unmanged code, it will be threadsafe.
-- bruce (sqlwork.com)
"Diffident" <Di*******@disc ussions.microso ft.com> wrote in message news:01******** *************** ***********@mic rosoft.com... | Hello All, | | If I create a class based on Singleton design pattern, are the methods in | this class thread-safe? | | Consider the following class: | | Public class Singleton | { | | public int Add(int a, int b) | { | | } | | } | | If suppose I have a request A which calls the method with params as 1 and 2 | and another request simultaneously calls the method with params as 3 and 4 | | Is there a possibility that the params are overrided as follows | | --> Request A calls the method with params as 1 and 4 | --> Request A calls the method with params as 3 and 2 | | Thanks for your suggestions!!!
Hello Scott,
Thanks for the articles, Scott... This debate came up at work the other day.
Your articles will help to prove my case. :=)
--
Matt Berther http://www.mattberther.com 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.
See:
http://odetocode.com/Articles/313.aspx http://odetocode.com/Articles/314.aspx -- Scott http://www.OdeToCode.com/blogs/scott/ Hello All,
If I create a class based on Singleton design pattern, are the methods in this class thread-safe?
Consider the following class:
Public class Singleton { public int Add(int a, int b) { } }
If suppose I have a request A which calls the method with params as 1 and 2 and another request simultaneously calls the method with params as 3 and 4
Is there a possibility that the params are overrided as follows
--> Request A calls the method with params as 1 and 4 --> Request A calls the method with params as 3 and 2
Thanks for your suggestions!!!
these are the only cases that cause a singleton to not be threadsafe.
1) you can use unmaned code if you know its threadsafe
2) you can use shared/statics if you sync, and you expect the same data
across all threads.
-- bruce (sqlwork.com)
"Diffident" <Di*******@disc ussions.microso ft.com> wrote in message
news:16******** *************** ***********@mic rosoft.com...
| 1) Shared variables
| 2) Unmanaged code
|
| Are these the necessary and sufficient conditions for singleton class
| methods to be thread-safe?
|
| "bruce barker" wrote:
|
| > it depends on the code. if your class contains no shareded variables,
and
| > calls no unmanged code, it will be threadsafe.
| >
| > -- bruce (sqlwork.com)
| >
| >
| > "Diffident" <Di*******@disc ussions.microso ft.com> wrote in message
| > news:01******** *************** ***********@mic rosoft.com...
| > | Hello All,
| > |
| > | If I create a class based on Singleton design pattern, are the methods
in
| > | this class thread-safe?
| > |
| > | Consider the following class:
| > |
| > | Public class Singleton
| > | {
| > |
| > | public int Add(int a, int b)
| > | {
| > |
| > | }
| > |
| > | }
| > |
| > | If suppose I have a request A which calls the method with params as 1
and
| > 2
| > | and another request simultaneously calls the method with params as 3
and 4
| > |
| > | Is there a possibility that the params are overrided as follows
| > |
| > | --> Request A calls the method with params as 1 and 4
| > | --> Request A calls the method with params as 3 and 2
| > |
| > | Thanks for your suggestions!!!
| >
| >
| >
Cool! I'm happy to know they are useful!
--
Scott http://www.OdeToCode.com/blogs/scott/ Hello Scott,
Thanks for the articles, Scott... This debate came up at work the other day. Your articles will help to prove my case. :=)
-- Matt Berther http://www.mattberther.com 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.
See:
http://odetocode.com/Articles/313.aspx http://odetocode.com/Articles/314.aspx -- Scott http://www.OdeToCode.com/blogs/scott/ Hello All,
If I create a class based on Singleton design pattern, are the methods in this class thread-safe?
Consider the following class:
Public class Singleton { public int Add(int a, int b) { } } If suppose I have a request A which calls the method with params as 1 and 2 and another request simultaneously calls the method with params as 3 and 4
Is there a possibility that the params are overrided as follows
--> Request A calls the method with params as 1 and 4 --> Request A calls the method with params as 3 and 2
Thanks for your suggestions!!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Neil Zanella |
last post by:
Hello,
I would be very interested in knowing how the following C++ multi-instance
singleton (AKA Borg) design pattern based code snippet can be neatly coded
in Python. While there may be somewhat unusual places where multi-instance
singleton is more useful than plain singleton, it seems to me that the
former leads to less coding, so unless I can somehow package the
singleton pattern in a superclass (so I don't have to code it...
|
by: Tim Clacy |
last post by:
Is there such a thing as a Singleton template that actually saves
programming effort?
Is it possible to actually use a template to make an arbitrary class a
singleton without having to:
a) explicitly make the arbitrary class's constructor and destructor private
b) declare the Singleton a friend of the arbitrary class
|
by: Alicia Roberts |
last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?
Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?
If someone knows about the...
|
by: Sharon |
last post by:
I wish to build a framework for our developers that will include a singleton
pattern.
But it can not be a base class because it has a private constructor and
therefore can be inherit.
I thought maybe a Template can be use for that, but C# does not support
Templates (will be C# generics in mid 2005).
Does anyone have a solution on how the singleton pattern can be written, in
C#, as a framework/ infrastructure class, so users can use this...
|
by: Robert W. |
last post by:
At the beginning of my C# days (about 6 months ago) I learned about the
Singleton pattern and implemented for Reference data, such as the kind that
appears in an Options dialog box. My Singleton code looks like this:
public sealed class Reference
{
private static readonly Reference instance = new Reference();
// Make the default constructor private, so that nothing can directly
create it.
| |
by: Paul Bromley |
last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class have
a number of paramterised constructors enabling me to pass in parameters or
not?
I am trying to use the following to send in a parmeter to a constructor, but
getting an error with it. I have a feeling that I am not...
|
by: baba |
last post by:
Hi all,
I'm quite new to C#.
I am trying to implement some basics reusable classes using this language
and the .NET Framework technology.
What I'm trying to do now is to implement a singleton class.
I did have a look at the Microsoft "Patterns and Practices" article
"Implementing Singleton in C#"
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp)
|
by: weaknessforcats |
last post by:
Design Pattern: The Singleton
Overview
Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it.
The global point of access is the object's Instance() method.
Individual users need to be prevented from creating their own instances of the Singleton.
|
by: Bob Johnson |
last post by:
Just wondering the extent to which some of you are implementing classes as
Singletons. I'm working on a brand new project and, early on, identified
some obvious candidates. By "obvoius candidates" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented as...
|
by: Lie |
last post by:
This is probably unrelated to Python, as this is more about design
pattern. I'm asking your comments about this design pattern that is
similar in functionality to Singleton and Borg: to share states.
I'm thinking about this design pattern (I don't know if anyone has
ever thought of this pattern before):
class OddClass(object):
def __init__(self):
global OddClass
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |