-1

I am trying to implement a singleton class that has to be used in two different threads, one sets up its member variables and the other one uses them, but the constructor is being called twice so I cannot use the members set in the first thread because they have their default value.

The implementation is quite standard, a static function that returns a reference to a static member in that function, I have tried to return a pointer, create a raw pointer and return it and the constructor always is called twice.

class Singleton
{
public:
    static Singleton &getInstance()
    {
        static Singleton instance;
        return instance;
    }

    // public methods

private:
    Singleton();
    Singleton(const Singleton &) = delete;
    Singleton(Singleton &&) = delete;
    Singleton &operator=(const Singleton &) = delete;
    Singleton &operator=(Singleton &&) = delete;

    // member variables
};

The thread trying to access to the singleton has been created using std::async(std::launch::async, lambda)

I expected that instance's constructor is called just the first time I call this function, but the second time instance has a different memory direction.

  • 2
    Without a [mcve] it's going to be hard to help you. Also please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 28 '19 at 09:09
  • 1
    Singleton are namespace in c++. Remove your class, declare data member as global variables. Member function as free function and you are done. – Oliv Jan 28 '19 at 09:10
  • One problem with a global variable is that it will always be instantiated before program start. The code above will only instantiate the singleton on demand. That might be an important requirement for the OP. – john Jan 28 '19 at 09:18
  • 1
    @Oliv Global variables and global functions should be avoided. And even namespace-scope variables have problems (for example initialization order between translation units). And classes and namespaces are not equal, not even for static-only classes or singletons. – Some programmer dude Jan 28 '19 at 09:29

1 Answers1

0

The C++11 standard guarantees that static local variables are only created once. This leaves us with the following options:

  1. Your compiler isn't C++11-compliant. Could be because it's old or you're using the wrong options.
  2. Your Singleton throws an (uncaught) exception, and the initialization is attempted again.
  3. Your constructor calls getInstance(), which results in undefined behavior.

The above assumes you've implemented the Singleton pattern correctly. You could've also just messed up in some way, but I can't tell because you haven't provided the full code.

All the info taken from cppreference on static local variables.

ARentalTV
  • 344
  • 1
  • 12
  • 1
    There's a fourth possibility: That the OP creates a second instance of the `Singleton` class somewhere else. – Some programmer dude Jan 28 '19 at 11:34
  • That's a lot more likely I guess, you're right :P @Someprogrammerdude – ARentalTV Jan 28 '19 at 11:39
  • Edited to add the singleton implementation. The second time getInstance() is called is from a dynamic library, maybe the "local" space cannot be accessed from this library? And no, I am not creating a second instance from somewhere else, I have been debugged it. – Alberto-Izquierdo Jan 28 '19 at 12:33
  • @Alberto-Izquierdo Ah that probably explains it. Dynamic libraries and any kind of global data (including local static variables) are going to misbehave. – Some programmer dude Jan 28 '19 at 12:40