title: Visual C++ Threads Simple Example
body: <p>I am trying to create a basic thread from main by passing a function to _beginthread.
  But My output is not getting completed.</p>
  
  <p>I am getting the following output:</p>
  
  <pre><code>Starting thread
  48
  Main ends
  I
  </code></pre>
  
  <p>Could someone please clarify what is wrong in the following code?   </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;process.h&gt;
  using namespace std;
  
  void test(void *param)
  {
      cout &lt;&lt; "In thread function" &lt;&lt; endl;
      Sleep(1000); // sleep for 1 second
      cout &lt;&lt; "Thread function ends" &lt;&lt; endl;
      _endthread();
  }
  
  
  int main()
  {
      cout &lt;&lt; "Starting thread" &lt;&lt; endl;
      cout &lt;&lt; _beginthread(test,0,NULL);
      cout &lt;&lt; "Main ends" &lt;&lt; endl;
      return 0;
  }
  </code></pre>