// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// C++ Source Code File Name: testprog.cpp
// Compiler Used: MSVC40, egcs-2.90.29, HP CPP 10.24
// Produced By: Doug Gaer   
// File Creation Date: 03/25/2000
// Date Last Modified: 05/26/2000
// Copyright (c) 2000 Douglas M. Gaer
// ----------------------------------------------------------- // 
// ------------- Program Description and Details ------------- // 
// ----------------------------------------------------------- // 
/*
The VBD and VBS C++ classes are copyright (c) 1997 and 2000 by 
Douglas M. Gaer. All those who put this code or its derivatives 
in a commercial product MUST mention this copyright in their
documentation for users of the products in which this code or its 
derivative classes are used. Otherwise, you have the freedom to 
redistribute verbatim copies of this source code, adapt it to your 
specific needs, or improve the code and release your improvements 
to the public provided that the modified files carry prominent 
notices stating that you changed the files and the date of any
change.

THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.

Simple test program demonstrating how to join a vbThread.
*/
// ----------------------------------------------------------- //   
#include <iostream.h>
#include <stdlib.h>
#include "vbthread.h"

// Constants
const int THREAD_EXIT_CODE = 15; // Arbitrary exit code value

// Global variables
int thread_count = 0;

class SimpleThread : public vbThread
{
public:
  SimpleThread() { }
  ~SimpleThread() { }
  
private: // Base class interface
  void *ThreadEntryRoutine(vbThread_t *thread);
};

void *SimpleThread::ThreadEntryRoutine(vbThread_t *thread)
{
  cout << "Excuting thread: " << thread_count++ << "\n" << flush;
  return (void *)THREAD_EXIT_CODE;
}

// Main thread of execution
int main()
{
  SimpleThread t;

  const int num_threads = 10;
  vbThread_t *thread[num_threads];

  int i;
  for(i = 0; i < num_threads; i++) {
    thread[i] = t.CreateThread();
    t.sSleep(1);
  }

  // Wait for all the threads to finish before exiting
  for(i = 0; i < num_threads; ++i) {
    if(t.JoinThread(thread[i]) != 0)
      cout << "Could not join the thread" << endl;
    if(thread[i]->GetThreadExitCode() != THREAD_EXIT_CODE)
      cout << "This thread exited with an error" << endl;
  }
  
  return 0; // Exit the process
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //