473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Singleton design pattern

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!!!
Nov 19 '05 #1
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!!!
Nov 19 '05 #2
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!!!

Nov 19 '05 #3
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!!!

Nov 19 '05 #4
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!!!

Nov 19 '05 #5
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!!!
| >
| >
| >
Nov 19 '05 #6
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!!!

Nov 19 '05 #7

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

Similar topics

4
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...
7
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)...
3
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...
21
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...
13
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...
14
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...
2
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...
3
weaknessforcats
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...
2
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"...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
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...
0
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...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.