|
-
December 18th, 2009, 03:21 AM
#1
Static / Singleton Classes + Multiple Threads
Hey,
I was having a conversation with a guy at work the other day and we were talking about singletons and he mentioned that only one thread can access a singleton at any given time, so the entire class/instance is entirely locked out to any other threads...
Im currently working on a distributed node system, so i have lots of apps that can sit on various servers and just wait to be sent information then process it and send it back. Now a few of these use singletons to manage cached collections of related data but as the requests come asynchronously and apparently static and singleton classes cannot allow multiple threads to use them at the same time (even with manual lock handling in place) i just wanted to see if anyone could shed any more light on this. As ideally i want multiple threads and be able to read from these singletons as there is more reads than there are writes...
The other reason i ask is because im writing quite a few Extension methods, and im wondering if they suffer from the same problem... im hoping its hogwash and you can use singletons and just put your own locking mechanisms in place, but like always im sure im wrong
Last edited by Grofit; December 18th, 2009 at 05:01 AM.
-
December 18th, 2009, 05:39 AM
#2
Re: Static / Singleton Classes + Multiple Threads
 Originally Posted by Grofit
he mentioned that only one thread can access a singleton at any given time, so the entire class/instance is entirely locked out to any other threads.
He's wrong The only way to enforce this is to explicitly code that support by using lock statements (or similar). By default there is no thread safety supplied.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 18th, 2009, 06:26 AM
#3
Re: Static / Singleton Classes + Multiple Threads
Woohoo!
Thats a worry off my mind, thanks for the confirmation...
-
December 18th, 2009, 06:33 AM
#4
Re: Static / Singleton Classes + Multiple Threads
 Originally Posted by Mutant_Fruit
By default there is no thread safety supplied.
That's not completly true. This MSDN article says about the List<t> and Thread Safety the next:
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
And I can remember seeing this line in other articles as well
-
December 18th, 2009, 06:59 AM
#5
Re: Static / Singleton Classes + Multiple Threads
Yes, but is is enforced by programmer who coded it this way, there is nothing like default thread safety for static members. Only one which could be considered exception is that if the class is loaded for first time, the class (static) constructor is thread safe.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
December 18th, 2009, 07:01 AM
#6
Re: Static / Singleton Classes + Multiple Threads
 Originally Posted by dannystommen
That's not completly true. This MSDN article says about the List<t> and Thread Safety the next:
And I can remember seeing this line in other articles as well
Exactly as boudino said. Support for this was added by the MS programmers. The .NET runtime offers you no protection by default for accessing/modifying shared state with multiple threads.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|