home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,708 of 33,346   
   =?ISO-8859-1?Q?=D6=F6_Tiib?= to Seungbeom Kim   
   Re: Singletons   
   05 Dec 12 23:07:00   
   
   From: ootiib@hot.ee   
      
   On Wednesday, 5 December 2012 16:19:25 UTC+2, Seungbeom Kim  wrote:   
   > On 2012-11-30 10:08, Alexander Terekhov wrote:   
   > > Seungbeom Kim wrote:   
   > >> Can you give some real-world examples of the need for the   
   > >> initialization to be lazy, i.e. not earlier than the first use?   
      
   There are lot of cases where you can benefit from lazy initialization.   
   For example optional module ... say some search engine, dictionary or   
   spell-checker. You load it only if user actually does something where   
   that thing participates and so you save time and space. Lazyness is   
   not property of singletons, it is one of tools to achieve smooth   
   performance with anything.   
      
   > >> My understanding is that when you think of singletons as global   
   > >> objects, you shouldn't mind their initialization being earlier   
   > >> than their first use.   
      
   The things are unrelated. If initializing something that is likely   
   never used takes several seconds then it makes sense to delay its   
   initialization until it is more probable that it will be actually   
   used.   
      
   > > To begin with, search the usage of pthread_once() on the net for   
   > > examples.   
   >   
   > Sorry, but I don't get how pthread_once() answers my question.   
   > Can you be more specific, please?   
      
   He meant yet-another popular way of initializing single state lazily:   
      
     static IOneOfKind* theState = nullptr;   
      
     IOneOfKind* IOneOfKind::getInstance()   
     {   
         static pthread_once_t control;   
         pthread_once( control, initTheState );   
         return theState;   
     }   
      
   That does not answer your "why?" because it answers to "how?".   
      
   If the product you make is of any use then real soon they request   
   more. Single player? But we want to multiplay! Single view?   
   But we have three monitors! etc. Then the code with dependency   
   injection is reusable but the code full of getInstance()s has to be   
   thrown away and written anew. So ... if you like to write things   
   anew (who doesnt?) then use Singletons. ;)   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca