title: Lock std::map C++
body: <p>I have a problem with using <code>std:map</code> in my multithread application. I need to lock the map object when thread is writing to this object. Parallely another threads which reading this object should shop until writing process is finish.</p>
  
  <p>Sample code:</p>
  
  <pre><code>std::map&lt;int, int&gt; ClientTable;
  
  int write_function() //&lt;-- only one thread uses this function
  {
    while(true)
    {
      //lock ClientTable
      ClientTable.insert(std::pair&lt;int, int&gt;(1, 2)); // random values
      //unlock ClientTable
  
      //thread sleeps for 2 secs
    }
  }
  
  int read_function()  //&lt;--- many thread uses this function
  {
    while(true)
    {
       int test = ClientTable[2]; // just for test
    }
  }
  </code></pre>
  
  <p>How to lock this std::map object and correctly synchronise this threads?</p>