design - Warm up some variables in C++ -


i have c++ library works on numeric values, values not available @ compile time immediatly available @ runtime , based on machine-related details, in short need values display resolution, number of cpu cores , on.

the key points of question are:

  • i can't ask user input values ( both coders/user of lib , final user )
  • i need warm once application starts, it's 1 time thing
  • this values later used methods , classes

the possible solutions are:

  • build data structure data, declare data dummy dummy name of variable used store , contructor/s handle 1 time inizialization related values
  • wrap first solution in method warmup() , putting method right after start main() ( it's simple thing remember , use )

the big problems still unsolved are:

  • the user can declare more 1 data structure since data it's type , there no restrictions throwing 2-4-5-17 variables of same type in c++
  • the warmup() method can little intrusive in design of other classes, can happen warmup() method used in local methods , not in main().

i need force creation of 1 single instance of 1 specific type @ runtime when have no power on actual use of library, or @ least need design in way user understand kind of error going on keeping use of library intuitive , simple as possible.

can see solution ?


edit:

my problems more difficult due fact i'm trying multi-threading compatible data structure.

first, others have said, obvious (and best) answer singleton. since you've added multithreading requirement, however: there 2 solutions, depending on whether object modified code using singleton. (from description, gather not.) if not, sufficient use "naïve" implementation of singleton, , ensure singleton initialized before threads started. if no thread started before enter main (and consider bad practice otherwise), following largely sufficient:

class singleton {     static singleton const* ourinstance;     singleton();      singleton( singleton const& );     singleton& operator=( singleton const& );  public:     static singleton const& instance(); }; 

and in implementation:

singleton const* singleton::ourinstance = &singleton::instance();  singleton const& singleton::instance() {     if ( ourinstance == null ) {         ourinstance = new singleton;     }     return *ourinstance; } 

no locking necessary, since no thread modifying once threading starts.

if singleton mutable, have protect access it. above (without const, obviously), , leave locking client, in such cases, i'd prefer locking in instance function, , returning std::shared_ptr deleter frees lock, acquired in instance function. think following work (but i've never needed it, , haven't tried it):

class singleton {     static singleton* ourinstance;     static std::mutex ourmutex;      class lockforpointer     {     public:         operator()( singleton* )         {             singleton::ourmutex.unlock();         }     };     class lockforinstance     {         bool myownershipistransfered;     public:         lockforinstance             : myownershipistransfered( false )         {             singleton::ourmutex.lock();         }         ~lockforinstance()         {             if ( !myownershipistransfered ) {                 singleton::ourmutex.unlock();             }         }         lockforpointer transferownership()         {             myownershipistransfered = true;             return lockforpointer();         }     }; public:     static std::shared_ptr<singleton> instance(); }; 

and implementation:

static singleton* ourinstance = null; static std::mutex ourmutex;  std::shared_ptr<singleton> singleton::instance() {     lockforinstance lock;     if ( ourinstance == null ) {         ourinstance = new singleton;     }     return std::shared_ptr<singleton>( ourinstance, lock.transferownership() ); } 

this way, same lock used check null , accessing data.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -