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 31,933 of 33,346   
   Pete Becker to Hei   
   Re: Require Lock?   
   14 Feb 12 17:22:03   
   
   9aaeb47e   
   From: pete@versatilecoding.com   
      
   On 2012-02-14 10:38:38 +0000, Hei said:   
      
   > Hi,   
   > Here is my pesudocode:   
   > class B {};   
   > class A {   
   > public:   
   >   std::map m_myMap;   
   >   A::A() {   
   >       m_myMap[0] = new B();   
   >       pthread_t threadID;   
   >       pthread_create(&threadID, NULL, start, NULL);   
   >   }   
   >   static void* start(void*) {   
   >       // use m_myMap[0] here   
   >   }   
   > }   
   > I wonder whether m_myMap[0] may contain some invalid value since the   
   > new thread might be in another CPU that might not see the change to   
   > m_myMap[0] in the constructor yet (i.e. the change only 'exists' in   
   > one of the CPU's caches).   
   > Thanks in advance.   
      
   No idea (although it would be insane for this not to work). But if you create   
   the new thread using C++11's thread object, the invocation of the thread   
   object's constructor synchronizes with the beginning of the invocation of the   
   called function, which is    
   the formal language that says that all the changes you've made before creating   
   the thread show up in the new thread.   
      
   --   
   Pete   
   Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard   
   C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)   
      
      
         [ 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