title: Updating cache without blocking
body: <p>I currently have a program that has a cache like mechanism.  I have a thread listening for updates from another server to this cache.  This thread will update the cache when it receives an update.  Here is some pseudo code:</p>
  
  <pre><code>void cache::update_cache()
  {
      cache_ = new std::map&lt;std::string, value&gt;();
      while(true)
      {
          if(recv().compare("update") == 0)
          {
              std::map&lt;std::string, value&gt; *new_info = new std::map&lt;std::string, value&gt;();
              std::map&lt;std::string, value&gt; *tmp;
              //Get new info, store in new_info
              tmp = cache_;
              cache_ = new_cache;
              delete tmp;              
          }
      }
  }
  
  std::map&lt;std::string, value&gt; *cache::get_cache()
  {
      return cache_;
  }
  </code></pre>
  
  <p><code>cache_</code> is being read from many different threads concurrently.  I believe how I have it here I will run into undefined behavior if one of my threads call <code>get_cache()</code>, then my cache updates, then the thread tries to access the stored cache.</p>
  
  <p>I am looking for a way to avoid this problem.  I know I could use a mutex, but I would rather not block reads from happening as they have to be as low latency as possible, but if need be, I can go that route.</p>
  
  <p>I was wondering if this would be a good use case for a unique_ptr.  Is my understanding correct in that if a thread calls get_cache, and that returns a unique_ptr instead of a standard pointer, once all threads that have the old version of cache are finished with it(i.e leave scope), the object will be deleted.  </p>
  
  <p>Is using a unique_ptr the best option for this case, or is there another option that I am not thinking of?</p>
  
  <p>Any input will be greatly appreciated.</p>
  
  <p>Edit: </p>
  
  <p>I believe I made a mistake in my OP.  I meant to use and pass a shared_ptr not a unique_ptr for cache_.  And when all threads are finished with cache_ the shared_ptr should delete itself.</p>
  
  <p>A little about my program:  My program is a webserver that will be using this information to decide what information to return.  It is fairly high throughput(thousands of req/sec)  Each  request queries the cache once, so telling my other threads when to update is no problem.  I can tolerate slightly out of date information, and would prefer that over blocking all of my threads from executing if possible.  The information in the cache is fairly large, and I would like to limit any copies on value because of this.</p>
  
  <p><code>update_cache</code> is only run once.  It is run in a thread that just listens for an update command and runs the code.</p>