// ------------------------------- //
// -------- 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/09/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 used to demonstrate how to exit from a 
thread without terminating the whole process.
*/
// ----------------------------------------------------------- //   
#include <iostream.h>
#include "vbthread.h"

// Constants
const int THREADFAIL = 1;
const int THREADPASS = 0;
 
// Global variables
int err_condition = 0;

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

void *SimpleThread::ThreadEntryRoutine(vbThread_t *thread)
{
  cout << "Executing thread" << endl;  
  if(err_condition) {
    cout << "Reached error condition!" << endl;
    ExitThread(thread, THREADFAIL);
  }

  cout << "Exiting the thread with no errors reported" << endl;
  return ExitThread(thread, THREADPASS); 
}

void CheckExitCode(vbThread_t *thread)
{
  cout << "Checking the thread's status" << endl;
  if(int(thread->GetThreadExitCode()) == THREADPASS) 
    cout << "The thread passed" << endl;
  else if(int(thread->GetThreadExitCode()) == THREADFAIL)  
    cout << "The thread failed" << endl;
  else 
    cout << "Invalid thread exit code reported" << endl;

  cout << endl;
}

int main()
{
  SimpleThread t;
 
  // Create and start a thread
  vbThread_t *thread = t.CreateThread();
 
  // Wait for the thread to complete, and release its resources
  t.JoinThread(thread);
  CheckExitCode(thread); 
  delete thread;

  // Simulate and error condition
  err_condition = 1;
  thread = t.CreateThread();
  t.JoinThread(thread);
  CheckExitCode(thread); 

  return 0; // Exit the process
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //