title: Cannot get the threads to work in C++?
body: <p>I am trying to create a program that makes five simple threads with the PrintHello methods. I want to be able to run this program and see how the threads will work and when they will print out. I know that a thread splits the time between tasks. So a task might run for 1 second while the other task might only get .01 seconds but it gives the illusion that all programs were running at once.</p>
  
  <p>I want my program to be able to demonstrate how a thread will work and also would like it so that I can changed the number of threads to more or less than 5.</p>
  
  <p><strong>The first line creates an array of threads.</strong>
        pthread_t threads[NUM_THREADS];</p>
  
  <p><strong>the for loop iterates through the length of the array.</strong>
        for( i=0; i &lt; NUM_THREADS; i++ ){
           ...
        }</p>
  
  <p><strong>This line of code will create the thread:</strong>
        pthread_create(&amp;threads[i], NULL,PrintHello, (void *)i);</p>
  
  <p><strong>This will exit the thread.</strong>
        pthread_exit(NULL);</p>
  
  <p><strong>Errors that I am getting are listed at the bottom of this window.</strong></p>
  
  <p>Code:</p>
  
  <pre><code>#include "pthread.h"
  #define NUM_THREADS 5
  
  void *PrintHello(void *threadid)
  {
     long tid;
     tid = (long)threadid;
     cout &lt;&lt; "Hello World! Thread ID, " &lt;&lt; tid &lt;&lt; endl;
     pthread_exit(NULL);
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      pthread_t threads[NUM_THREADS];
      int rc;
      int i;
      for( i=0; i &lt; NUM_THREADS; i++ ){
        cout &lt;&lt; "main() : creating thread, " &lt;&lt; i &lt;&lt; endl;
        rc = pthread_create(&amp;threads[i], NULL, 
                            PrintHello, (void *)i);
        if (rc){
           cout &lt;&lt; "Error:unable to create thread," &lt;&lt; rc &lt;&lt; endl;
           exit(-1);
        }
     }
     pthread_exit(NULL);
      std::getchar();
      return 0;
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Error   1   error C1083: Cannot open include file: 'pthread.h': No such file or directory   c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  6   1   TestProject
      2   IntelliSense: cannot open source file "pthread.h"   c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  6   1   TestProject
      3   IntelliSense: identifier "pthread_exit" is undefined    c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  79  4   TestProject
      4   IntelliSense: identifier "pthread_t" is undefined   c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  84  2   TestProject
      5   IntelliSense: identifier "pthread_create" is undefined  c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  89  12  TestProject
      6   IntelliSense: identifier "pthread_exit" is undefined    c:\users\itpr13266\desktop\c++\testproject\testproject\testproject.cpp  96  4   TestProject
  </code></pre>