/***************************************************************************
                          thread_test.cpp  -  description
                             -------------------
    begin                : Tue Dec 14 1999
    copyright            : (C) 1999 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#if __BORLANDC__ > 0x500
#include <condefs.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <sptk3/CThread.h>
#include <sptk3/CDateTime.h>
#include <string>

using namespace std;
using namespace sptk;

class CMyThread : public CThread {
   string m_name;
public:

   // Constructor
   CMyThread(string threadName,bool politeMode=true);

   // The thread function. 
   virtual void threadFunction();
};

CMyThread::CMyThread(string threadName,bool politeMode) : CThread(threadName,politeMode) {
   // Put anything you need here to define your actual thread
   puts((name()+" is created").c_str());
}

// The thread function. Prints a message once a second till terminated
void CMyThread::threadFunction() {
   puts((name()+" is started").c_str());
   while (!terminated()) {
      puts(("Output from "+name()).c_str());
      msleep(1000);
   }
   puts((name()+" is terminated").c_str());
}

int main(int argc, char* argv[]) {
   CMyThread   thread1("Thread1");
   CMyThread   thread2("Thread2");

   thread1.run();
   thread2.run();

   puts("Waiting 5 seconds while threads are running..");

   CThread::msleep(5000);

   thread1.terminate();
   thread2.terminate();

   return 0;
}
