title: How to print to standard output from multiple threads using POCO in c++
body: <p>I want to create a simple program that starts 5 threads that print "hello world! This is thread [thread number]".</p>
  
  <p>Each print operation should be preceded by a random wait operation, to demonstrate that the threads are running concurrently. (The threads will print their message in a random order, as opposed to printing in order which would happen if the threads are running sequentially.)</p>
  
  <p>Here is the code that should achieve this:</p>
  
  <pre><code>Poco::Thread thread[5];
  
  class HelloRunnable: public Poco::Runnable
  {
  public:
      HelloRunnable(int arg)
      {
          n = arg;
      }
  
      int n;
  
      virtual void run()
      {
          usleep(rand()%100000);
          std::cout &lt;&lt; "Hello, world! This is thread " &lt;&lt; n &lt;&lt; std::endl;
      }
  };
  
  int main() 
  {
      for(int i=0; i&lt;5; i++)
      {
          HelloRunnable runnable(i);
          thread[i].start(runnable);
      }
  
      for(int i=0; i&lt;5; i++)
      {
          thread[i].join();
      }
      return 0;
  }
  </code></pre>
  
  <p>However, at run time, this gives the error:</p>
  
  <pre><code>//pure virtual method called
  //terminate called without an active exception
  //The program has unexpectedly finished.
  </code></pre>
  
  <p>If instead, I put <code>thread[i].join()</code> inside the same for loop as <code>thread[i].start()</code>, then the program runs without error but it prints the threads in order. (Becuase <code>join()</code> waits until the thread has finished before moving on, hence the threads are executed sequentially and not concurrently.</p>
  
  <p>How can I make the threads run concurrently and print out the message to the standard output as soon as <code>cout</code> is called?</p>