title: Pointer to Value in a std::map
body: <p>I have a std::map that is used by multiple threads to store data. The map is declared like this:</p>
  
  <pre><code>std::map&lt;int, Call&gt; calls;
  </code></pre>
  
  <p>From each thread, I have to acquire a mutex lock, obtain a pointer or reference to the object belonging to that thread, then release the mutex lock. I can modify the object after that because each object is used only by one thread. As soon as the thread dies, the corresponding pair in the map would also get deleted.</p>
  
  <p>I would like to know the best way to implement this. I was thinking of two ways:</p>
  
  <p>1) I know this one could look outrageously crazy, but still</p>
  
  <pre><code>std::map&lt;int, Call&gt; calls;
  ...
  
  {
      mutex.lock();
      Call* callptr = &amp;calls[id];
      mutex.unlock();
  
     // use callptr
  }
  </code></pre>
  
  <p>or 2) I think this one looks more sensible</p>
  
  <pre><code>std::map&lt;int, std::auto_ptr&lt;Call&gt; &gt; calls;
  
  ...
  
  {
      mutex.lock();
      std::auto_ptr&lt;Call&gt; callptr = map[id];
      mutex.unlock();
  
      // use callptr
  
      mutex.lock();
      map[id] = callptr;
      mutex.unlock();
  }
  </code></pre>
  
  <p>The threads actually are created in a different dll and I don't have the code for that. This dll that I'm writing now gets imported by that dll and used. So it has to be implemented with std::map only, but could anyone tell me if one of these methods is ok or if there are ways to make it more stable.</p>
  
  <p>Thanks</p>