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!!!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" <Diffident@.discussions.microsoft.com> wrote in message
news:0162F6D8-0528-4933-803D-8A8B3C9766AC@.microsoft.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" <Diffident@.discussions.microsoft.com> wrote in message
> news:0162F6D8-0528-4933-803D-8A8B3C9766AC@.microsoft.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" <Diffident@.discussions.microsoft.com> wrote in message
news:16C21497-2181-4436-B707-D06DB5BA6737@.microsoft.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" <Diffident@.discussions.microsoft.com> wrote in message
| > news:0162F6D8-0528-4933-803D-8A8B3C9766AC@.microsoft.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!!!
>>
0 comments:
Post a Comment