Thread: making a class only get constructed once

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't a key to MT to minimize the amount of shared data between the threads? As such wouldn't anything of which only one instance can exist be rather unsuitable to be used across threads?

    Cannot the same problem occur (more easily perhaps) with a single object passed by reference? What's to stop a programmer from constructing one where he thinks he needs it?
    At least the dependencies are more visible. As to constructing more than one (CWorld), you'd probably notice and you can limit construction...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course, if there is and should only ever ONE, there are two obvious solutions to avoid it being created in multiple threads:
    1. Create it before threads are started, in the main thread.
    2. Create it on the stack in the main function.
    Both or either can be used.

    Assuming that the class object isn't large, this should work fine, and avoids all sorts of problems.

    I agree that sharing should be avoided in a MT environment, as all shared data that is ever updated by any thread will be subject to locks to ensure that no other thread is getting inconsistent data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing this lazy instantiation is a bit of a problem. If you create a single instance normally or through Elysia's factory before other threads start, you won't have this problem.

    Using the singleton in multiple threads might be quite problematic though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that it also tends to be more difficult to design unit tests involving singletons.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    The multithreading problem is easy to solve using pthread_once or similar. But what will happen if someone calls Instantiate() from a shared lib/dll/whatever_your_OS supports? This imo depends on the behavior of the OS.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by pheres View Post
    The multithreading problem is easy to solve using pthread_once or similar. But what will happen if someone calls Instantiate() from a shared lib/dll/whatever_your_OS supports? This imo depends on the behavior of the OS.
    The same thing that will happen if they invoke the constructor on your non-Singleton solution.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    No, because they have no static flags which marks instantiated or not. These flags will either reside on a single one or more memory locations, depending on your OS. Try it out. Linux and WinXp should be enough to find a difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM