c++ - Problems with lazy initialization of singleton -


i want make singleton class has behavior below.

  1. my program has limited resources, don't want make singleton instance until need it. (lazy initialization)
  2. singleton class huge initialization takes long time.
  3. response time important in program.(like games)

but know, 1 & 2 & 3 conflict each other. in conditions, have choose 1 of them (memory or performance)

is there solutions program can meet memory & performance requirement?

"is there solutions program can meet memory & performance requirement?"

these requirements need satisfied regardless. ensure lazy instantiation of singleton class, recommend using scott meyer's singleton implementation guarantee lazy/thread safe initialization mentioned here:

class singleton { public:      static singleton& instance() {           static singleton theinstance;           return theinstance;      }      delete singleton(const singleton&);      delete singleton& operator=(const singleton&); private:      singleton() {}  }; 

about memory consumption you'll have optimize in different ways. can't tell, might have potential narrow memory footprint. there's not enough information given question far.


"but know, 1 & 2 & 3 conflict each other. in conditions, have choose 1 of them (memory or performance)"

i can't see actual conflicts?? there might performance hit, @ first access singleton::instance, subsequent accessing won't trigger initialization, what?


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -