title: How does thread join work?
body: <p>I found a C++ web server <a href="https://github.com/eidheim/Simple-Web-Server/" rel="nofollow">example</a> which works based on threads. </p>
  
  <p>I replaced <a href="https://github.com/eidheim/Simple-Web-Server/blob/master/https_examples.cpp#L161" rel="nofollow">line 161</a> from </p>
  
  <pre><code>server_thread.join();
  </code></pre>
  
  <p>to </p>
  
  <pre><code>std::cout&lt;&lt;"Before thread join\n";
  
  server_thread.join();
  
  std::cout&lt;&lt;"5 sec delay starting ...\n";
  this_thread::sleep_for(chrono::seconds(5));
  std::cout&lt;&lt;"5 sec delay ended\n";
  </code></pre>
  
  <p>The result obviously shows that the code after <code>join</code> is not running.</p>
  
  <pre><code>123
  {"firstName": "John","lastName": "Smith","age": 25}
  John Smith
  Before thread join
  </code></pre>
  
  <p>While in my simple example below, the code below <code>thread_2.join();</code> runs as well. While it does not run final <code>std::cout</code> until both threads are released. What is the logic behind <code>.join();</code> does it pause the current thread?</p>
  
  <p>If I want the code after <code>server_thread.join();</code> continues running alongside with the server, what is the proper solution?</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;boost/thread.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  void task1() { 
      // do stuff
      for(int i=0;i&lt;10;i++)
      {
          std::cout&lt;&lt;"task1 "&lt;&lt;"["&lt;&lt;i&lt;&lt;"]\n";
          std::this_thread::sleep_for(std::chrono::seconds(1));
      }
  }
  
  void task2()
  { 
      for(int i=0;i&lt;10;i++)
      {
          std::cout&lt;&lt;"task2 "&lt;&lt;"["&lt;&lt;i&lt;&lt;"]\n";
          std::this_thread::sleep_for(std::chrono::seconds(1));
      }
  }
  
  int main ()
  {
      using namespace boost; 
      std::thread thread_1 = std::thread(task1);
      std::thread thread_2 = std::thread(task2);
  
      // do other stuff
      thread_2.join();
      thread_1.join();
      // std::this_thread::sleep_for(std::chrono::seconds(10));
      std::cout&lt;&lt;"end of main function\n";
      return 0;
  }
  </code></pre>
  
  <p>results:</p>
  
  <pre><code>task1 [0]
  task2 [0]
  task1 [1]
  task2 [1]
  task1 [2]
  task2 [2]
  task1 [task2 [33]
  ]
  task2 [4]
  task1 [4]
  task2 [5]
  task1 [5]
  task2 task1 [[66]
  ]
  task2 [task1 [77]
  ]
  task2 task1 [[88]
  ]
  task2 [9]
  task1 [9]
  end of main function
  </code></pre>