title: Lambdas and threads
body: <p>I've recently started using lambdas an awful lot within threads, and want to make sure I'm not setting myself up for thread-safety issues/crashes later. My usual way of using them is:</p>
  
  <pre><code>class SomeClass {  
      int someid;  
      void NextCommand();  
      std::function&lt;void(int, int)&gt; StoreNumbers;  
      SomeClass(id, fn); // constructor sets id and storenumbers fn  
  }
  
  // Called from multiple threads  
  static void read_callback(int fd, void* ptr)  
  {  
      SomeClass* sc = static_cast&lt;SomeClass*&gt;ptr;  
      ..  
      sc-&gt;StoreNumbers(someint,someotherint); // voila, thread specific storage.  
  }  
  
  static DWORD WINAPI ThreadFn(LPVOID param)  
  {  
      std::list&lt;int&gt; ints1;  
      std::list&lt;int&gt; ints2;  
      auto storenumbers = [&amp;] (int i, int i2) {  
          // thread specific lambda.  
          ints1.push_back(i);  
          ints2.push_back(i2);  
          };  
      SomeClass s(id, storenumbers);  
      ...  
      // set up something that eventually calls read_callback with s set as the ptr. 
  }  
  </code></pre>
  
  <p>ThreadFn is used as the thread function for 30-40 threads.</p>
  
  <p>Is this acceptable? I usually have a few of these thread-specific lambdas that operate on a bunch of thread specific data.</p>
  
  <p>Thank you!</p>