CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    357

    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.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Static / Singleton Classes + Multiple Threads

    Quote Originally Posted by Grofit View Post
    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.

  3. #3
    Join Date
    Nov 2006
    Posts
    357

    Re: Static / Singleton Classes + Multiple Threads

    Woohoo!

    Thats a worry off my mind, thanks for the confirmation...

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Static / Singleton Classes + Multiple Threads

    Quote Originally Posted by Mutant_Fruit View Post
    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

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Static / Singleton Classes + Multiple Threads

    Quote Originally Posted by dannystommen View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured