title: thread const error C++
body: <p>Could anyone please tell me why this code is producing an error?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;stdexcept&gt;
  using namespace std;
  
  void thread_func(const std::map&lt;string, int&gt;&amp; shared) {
      for (int i = 0; i &lt; 10000; ++i) {
          if (shared["what"] != 2 || shared["when"] != 4) {
              throw std::logic_error("not read safe");
          }
      }
  }
  
  int main() {
      std::map&lt;string, int&gt; shared;
      shared["what"] = 2;
      shared["when"] = 4;
  
      vector&lt;thread&gt; threads;
      for (int i = 0; i &lt; 100; ++i) {
          threads.push_back(thread(thread_func, shared));
      }
  
      for (auto&amp; th : threads) {
          th.join();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This produces the following error</p>
  
  <pre><code>error: passing ‘const std::map&lt;std::basic_string&lt;char&gt;, int&gt;’ as ‘this’ argument of ‘std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::key_type&amp;&amp;) [with _Key = std::basic_string&lt;char&gt;; _Tp = int; _Compare = std::less&lt;std::basic_string&lt;char&gt; &gt;; _Alloc = std::allocator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, int&gt; &gt;; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type = int; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::key_type = std::basic_string&lt;char&gt;]’ discards qualifiers [-fpermissive]
           if (shared["what"] != 2 || shared["when"] != 4) {
  </code></pre>
  
  <p>Could someone please guide me in the right direction with this?</p>
  
  <p>Thanks</p>