Skip to content

SingletonPtr - name not accurate, thread-safety and initialization/destruction #2480

@0xc0170

Description

@0xc0170
  1. The name is not accurate
    The classes name SingletonPtr is confusing. This is not a singleton. We can create multiple instances of the same class. Therefore similar pattern in another code base is called LazyInstance for example.

  2. Thread safety
    This is not thread-safe, is it?:

            if (NULL == _ptr) {
            singleton_lock();
            _ptr = new (_data) T;
            singleton_unlock();
    }
    
  3. Initialization of T type
    It's not clear what SingletonPtr guarantees regarding initialization of a T object.

            if (NULL == _ptr) {
                singleton_lock();
                _ptr = new (_data) T;
                singleton_unlock();
    }
    

    If T is POD, its data are not initialized (at least C++03,C++98). should it be _ptr = new (_data) T(); ?

  4. The destructor for the T object
    There could be a method to destroy T object (call explicitly dtor).

Code reference: https://github.com/ARMmbed/mbed-os/blob/d89c3c18f9be97880f9173e87772ea2808fa8df1/hal/api/SingletonPtr.h

@pan- @c1728p9 @geky @sg-

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions