title: c++ compile std::thread example with scons
body: <p>I can't get scons to properly compile a small threading example (on Linux).</p>
  
  <p>If I run scons, it does this:</p>
  
  <pre><code>jarrett@jarrett-laptop:~/projects/c++_threads$ scons
  scons: Reading SConscript files ...
  scons: done reading SConscript files.
  scons: Building targets ...
  g++ -o build/main.o -c -std=c++11 -pthread -Wall -g src/main.cpp
  g++ -o build/c++threads build/main.o
  scons: done building targets.
  </code></pre>
  
  <p>then if I run <code>./build/c++threads</code> it throws this error:</p>
  
  <pre><code>terminate called after throwing an instance of 'std::system_error'
    what():  Operation not permitted
  Aborted
  </code></pre>
  
  <p>If i compile from the command line with this:</p>
  
  <pre><code>g++ -std=c++11 -pthread -Wall -g src/main.cpp
  </code></pre>
  
  <p>it compiles to <code>a.out</code>, and if I run <code>a.out</code> it runs the program (does some output for threads, etc).</p>
  
  <p>Here's my SConstruct file:</p>
  
  <pre><code># Tell SCons to create our build files in the 'build' directory
  VariantDir('build', 'src', duplicate=0)
  
  # Set our source files
  source_files = Glob('build/*.cpp', 'build/*.h')
  
  # Set our required libraries
  libraries = []
  library_paths = ''
  
  env = Environment()
  
  # Set our g++ compiler flags
  env.Append( CPPFLAGS=['-std=c++11', '-pthread', '-Wall', '-g'] )
  
  # Tell SCons the program to build
  env.Program('build/c++threads', source_files, LIBS = libraries, LIBPATH = library_paths)
  </code></pre>
  
  <p>and here's the cpp file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  
  //This function will be called from a thread
  
  void func(int tid) {
      std::cout &lt;&lt; "Launched by thread " &lt;&lt; tid &lt;&lt; std::endl;
  }
  
  int main() {
      std::vector&lt;std::thread&gt; th;
  
      int nr_threads = 10;
  
      //Launch a group of threads
      for (int i = 0; i &lt; nr_threads; ++i) {
          th.push_back(std::thread(func,i));
      }
  
      //Join the threads with the main thread
      for(auto &amp;t : th){
          t.join();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Anyone have any idea what I'm doing wrong???</p>
  
  <p>Appreciate any help!</p>
  
  <p>Cheers</p>
  
  <p>Jarrett</p>