Singleton design pattern

G

Guest

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!!!
 
B

bruce barker

it depends on the code. if your class contains no shareded variables, and
calls no unmanged code, it will be threadsafe.

-- bruce (sqlwork.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!!!
 
G

Guest

1) Shared variables
2) Unmanaged code

Are these the necessary and sufficient conditions for singleton class
methods to be thread-safe?
 
M

Matt Berther

Hello Scott,

Thanks for the articles, Scott... This debate came up at work the other day.
Your articles will help to prove my case. :=)
 
B

bruce barker

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)




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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top