// ------------------------------- //
// -------- 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.

Test program demonstrating how to set thread priorities using the
within a thread pool.
*/
// ----------------------------------------------------------- //   
#include <iostream.h>
#include "vbthread.h"

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

void *SimpleThread::ThreadEntryRoutine(vbThread_t *thread)
// Thread's entry function
{
  cout << "Executing worker thread: " << (int)thread->GetThreadParm() << ", "
       << thread->ThreadPriorityMessage() << '\n' << flush;
  return 0;
}

int main()
{
  const unsigned NUM_WORKERS = 10;

  SimpleThread *t = new SimpleThread;
  thrPool *pool = new thrPool;
  vbThread_t *thread[NUM_WORKERS];
  
  unsigned i;
  // Consturct some new threads without executing them
  for(i = 0; i < NUM_WORKERS; i++) thread[i] = t->ConstructThread((void *)i);

  // Change some of the thread priorities before placing them in the pool
  thread[NUM_WORKERS-1]->SetThreadPriority(vbTHREAD_PRIORITY_HIGH);
  thread[NUM_WORKERS-2]->SetThreadPriority(vbTHREAD_PRIORITY_HIGH);
  thread[NUM_WORKERS-3]->SetThreadPriority(vbTHREAD_PRIORITY_HIGH);

  thread[0]->SetThreadPriority(vbTHREAD_PRIORITY_LOW);
  thread[1]->SetThreadPriority(vbTHREAD_PRIORITY_LOW);
  thread[2]->SetThreadPriority(vbTHREAD_PRIORITY_LOW);

  // Put the threads in the thread pool
  for(i = 0; i < NUM_WORKERS; i++) pool->AddThread(thread[i]);

  // Execute all the threads in the pool
  thrPoolNode *ptr = pool->GetHead();
  while(ptr) {
    t->CreateThread(ptr->GetThreadPtr());
    t->sSleep(1);
    ptr = ptr->GetNext();
  }

  // Wait for the treads to finish before destroying the pool
  t->JoinThread(pool);

  cout << endl;
  cout << "Destroying the thread pool..." << endl;
  t->DestoryThreadPool(pool);

  cout << "Exiting..." << endl;

  delete t;
  return 0; // Exit the process, terminating all threads
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //