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,929 of 33,346   
   Henning Follmann to Hei   
   Re: Require Lock?   
   14 Feb 12 17:17:18   
   
   9aaeb47e   
   From: hfollmann@itcfollmann.com   
      
   On 2012-02-14, Hei  wrote:   
   > 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.   
   >   
   >   
      
   Well, yes you do.   
   And more. Usually I wouldn't create threads in a constructor. Also your   
   threadID is not accessible after you finished A::A().   
      
   -H   
      
   --   
   Henning Follmann           | hfollmann@itcfollmann.com   
      
      
         [ 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