title: C++11 thread to modify std::list
body: <p>I'll post my code, and then tell you what I think it's doing.</p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;list&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  ...
  
  //List of threads and ints
  list&lt;thread&gt; threads;
  list&lt;int&gt; intList;
  
  //Whether or not a thread is running
  bool running(false);
  
  //Counters
  int busy(0), counter(0);
  
  //Add 10000 elements to the list
  for (int i = 0; i &lt; 10000; ++i){
      //push back an int
      intList.push_back(i);
      counter++;
      //If the thread is running, make a note of it and continue
      if (running){
          busy++;
          continue;
      }
      //If we haven't yet added 10 elements before a reset, continue
      if (counter &lt; 10)
          continue;
      //If we've added more than 10 ints, and there's no active thread,
      //reset the counter and launch
      counter = 0;
      threads.push_back(std::thread([&amp;]
          //These iterators are function args
          (list&lt;int&gt;::iterator begin, list&lt;int&gt;::iterator end){
          //mutex for the running bool
          mutex m;
          m.lock();
          running = true;
          m.unlock();
  
          //Remove either 10 elements or every element till the end
          int removed(0);
          while (removed &lt; 10 &amp;&amp; begin != end){
              begin = intList.erase(begin);
              removed++;
          }
  
          //unlock the running bool
          m.lock();
          running = false;
          m.unlock();
          //Pass into the thread func the current beginning and end of the list
      }, intList.begin(), intList.end()));
  }
  
  for (auto&amp; thread : threads){
      thread.join();
  }
  </code></pre>
  
  <p>What I think this code is doing is adding 10000 elements to the end of a list. For every 10 we add, launch a (single) thread that deletes the first 10 elements of the list (at the time the thread was launched). </p>
  
  <p>I don't expect this to remove every list element, I was just interested in seeing if I could add to the end of a list while removing elements from the beginning. In Visual Studio I get a "<code>list iterators incompatible</code>" error quite often, but I figure the problem is cross platform.</p>
  
  <p>What's wrong with my thinking? I know it's something</p>
  
  <p>EDIT:</p>
  
  <p>So I see now that this code is very incorrect. Really I just want one auxiliary thread active at a time to delete elements, which is why I though calling erase was ok. However I don't know how to declare a thread without joining it up, and if I wait for that then I don't really see the point of doing any of this.</p>
  
  <p>Should I declare my thread before the loop and have it wait for a signal from the main thread?</p>
  
  <p>To clarify, my goal here is to do the following: I want to grab keyboard presses on one thread and store them in a list, and every so often log them to a file on a separate thread while removing the things I've logged. Since I don't want to spend a lot of time writing to the disk, I'd like to write in discrete chunks (of 10). </p>
  
  <p>Thanks to Christophe, and everyone else. Here's my code now... I may be using lock_guard incorrectly.</p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;list&gt;
  #include &lt;iostream&gt;
  #include &lt;atomic&gt;
  
  using namespace std;
  
  ...
  
  atomic&lt;bool&gt; running(false);
  list&lt;int&gt; intList;
  int busy(0), counter(0);
  mutex m;
  thread * t(nullptr);
  
  for (int i = 0; i &lt; 100000; ++i){
      //Would a lock_guard here be inappropriate?
      m.lock();
      intList.push_back(i);
      m.unlock();
      counter++;
      if (running){
          busy++;
          continue;
      }
      if (counter &lt; 10)
          continue;
      counter = 0;
      if (t){
          t-&gt;join();
          delete t;
      }
      t = new thread([&amp;](){
          running = true;
  
          int removed(0);
          while (removed &lt; 10){
              lock_guard&lt;mutex&gt; lock(m);
              if (intList.size())
                  intList.erase(intList.begin());  
              removed++;
          }
          running = false;
      });
  }
  
  if (t){
      t-&gt;join();
      delete t;
  }
  </code></pre>