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 31,397 of 33,346    |
|    Andy Venikov to darkknight.21@gmail.com    |
|    Re: re-ordering across a mutex    |
|    24 Aug 11 16:44:20    |
   
   From: swojchelowek@gmail.com   
      
   On 08/18/2011 05:32 AM, darkknight.21@gmail.com wrote:   
   > I have a follow-up question to my question on the double checked   
   > locking problem   
   >   
   > If I use a mutex of some kind like this   
   > acquire-mutex   
   > execute code/ modify shared variables   
   > release-mutex   
   >   
   > For this to work, the compiler is not allowed to move code across the   
   > acquire/release statements. Does the documentation for a mutex   
   > usually say this?   
   >   
   > Anyway, assuming the above is true, is not a simple solution to the   
   > singleton problem something like this   
   > if (ptr == 0) // line1 - inexpensive   
   > {   
   > acquire-mutex1   
   > if (ptr == 0)   
   > {   
   > ptr1 =&singleton; // whatever   
   > acquire_mutex2;   
   > ptr = ptr1;   
   > release_mutex2;   
   > }   
   > release-mutex1   
   > }   
   >   
   > At line1, if ptr is non-zero, we know for sure that the singleton   
   > object has been fully constructed. No volatile anywhere. The mutexes   
   > would also provide the needed memory barriers.   
   >   
   > Would this work?   
   >   
   >   
      
   No, because generally mutexes won't let the code escape the protected   
   region, but code outside of the protected region can still be moved   
   inside of the protected scope. With DLCP it usually means that the   
   construction of the singleton may happen after it's address was   
   assigned to a shared pointer. That means that a thread checking ptr   
   might try to access unconstructed value.   
   But that's not even the only reason why   
   it's not going to work. You should read the article you quoted in your   
   earlier e-mail more carefully, it explains everything.   
   The moment you try to use "ptr" outside of the protected scope, you're   
   on your own.   
      
   Andy.   
      
      
   --   
    [ 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