c++ - Problems with lazy initialization of singleton -
i want make singleton class has behavior below.
- my program has limited resources, don't want make singleton instance until need it. (lazy initialization)
- singleton class huge initialization takes long time.
- 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
Post a Comment