Rx++ Scheduler again, question on singleton pattern used

Dec 6, 2012 at 1:09 AM

I've just read that, following C++ 11 specification, Meyer's singleton is now perfectly safe.

Additionally, when running under VS 2012, I have not noticed any ill effects on exit, even if I must add that my tests are pretty simple, so far. Could somebody please explain what exactly do the following code comments mean:

    struct DefaultScheduler
    {
    private: DefaultScheduler(const DefaultScheduler&);
    public:
        static DefaultScheduler& Instance()
        {
            // TODO: leaks. race condition on atexit though.
            static DefaultScheduler* instance = new DefaultScheduler;
            return *instance;
        }

 

Dec 6, 2012 at 8:15 AM

Looking at the code, the DefaultScheduler is never deleted - the destructor is never called.

There are chances that a new task will be run when the app is shutting down with unexpected results.

 

 


Dec 6, 2012 at 2:18 PM
Edited Dec 6, 2012 at 2:40 PM

Right, I see what you mean! 

Even if C runtime is specified to destroy a static object in atexit, this is not a 'simple' object. The issues are not singleton related but to the fact that it runs a thread servicing queued tasks.

Actually, I've seen it myself that, if you don't wrap program's main inside a scheduler call, you will miss a lot of scheduled work as the main exits.

In contrast, the following C# program prints all 1,000,000 integers:

 class Program
 {
        static void Main(string[] args)
        {
            RangeTest();
        }

        static void RangeTest()
        {
            var observableRange = Observable.Range(1,1000000);

            Action<int> observer = x => Console.WriteLine(x);

            observableRange.Subscribe(observer);
        }
}