Thread: Initialising data when application initialises?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks for the reply. No--not really. (* See note at bottom)

    What I'm asking is a very general question. Really, it is this:

    How do cause some function to be called on main thread initialisation?

    I can call it through the use of a dummy variable initialisation, like so:

    int dummy = some_func();

    But is there a better way? I.e. without having dummy variables every where?

    I'm struggling to explain myself without specific examples. So here's another one -- a singleton:

    Code:
    SomeClass& SomeClass::singleton()
    {
      static SomeClass sc;
      return sc;
    }
    Now this is OK--except in a multiple threaded application where the singleton() method may be called for the first time by two threads simultaneously, resulting in a race condition during the creation of the static "sc" variable.

    I can overcome this by ensuring the singleton() method is initially called in the application's main thread by:

    SomeClass& dummy = SomeClass::singleton();

    in the cpp file.

    However, do I really need to declare a dummy variable in order to get the function to be called on initialisation?

    Is there some way in C++ I can get SomeClass::singleton() to be called on start-up without the use of dummy data?

    * PS

    Thanks for the example Bench82 -- I see what your code is doing now. However, the answer is still no--it's not what I want. I still want a function called on main thread start-up because I need to ensure it is called for the first time in the main thread an not in any other thread. Also, remember, I am writing an api so it is not applicable to call functions in the main() C function.
    Last edited by Davros; 01-01-2007 at 08:48 PM.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. Intercepting Data Bound for Local Application from Remote Server
    By maththeorylvr in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-29-2005, 01:57 AM
  4. Dynamic data members?
    By confusalot in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:15 AM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM