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 1149
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*******@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.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*******@discussions.microsoft.com> wrote in message news:01**********************************@microsof t.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*******@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.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*******@discussions.microsoft.com> wrote in message
| > news:01**********************************@microsof t.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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |