Thread safety in C++
<p>I have question regarding thread safety as below ( I have only two threads in which one of the threads only read from the map, the other threads would be writing and reading as shown):</p>

<pre><code>//Thread 2: the reading and writing thread
unordered_map&lt;int, unordered_map&lt;classA*&gt;*&gt;testMap;

//need lock because writing to the map?
testMap[1] = new unordered_map&lt;int, classA*&gt;;

//do not need lock because only reading and the other thread is only reading?
unordered_map&lt;classA*&gt;* ptr = testMap[1];

//need lock because writing?
(*ptr)[1] = new classA;

//do not need lock because only reading and the other thread is only reading?
classA* ptr2 = (*ptr)[1];

//din't modify the map, but modify the data pointed by the pointer stored by the map, do I need lock?
ptr2-&gt;field1 = 5;
ptr2-&gt;field2 = 6;

//end of reading and writing thread
</code></pre>

<p>What is the correct way to lock to unordered_map? Also, should I use a single lock or multiple locks?</p>

<p>Thanks.</p>
