//------------------------------------------------------------------------ // Filename: // alarmclock-Thrd.cpp // PROGRAM DESCRIPTION // Class implementation file for DriverThread and SleeperThread class //------------------------------------------------------------------------ #include #include "ThreadClass.h" #include "alarmclock-Thrd.h" #include "alarmclock-mon.h" // static data variables static AlarmMonitor Alarm("AlarmClock"); // ----------------------------------------------------------------------- // FUNCTION Filler(): // This function fills a strstream with spaces. // ----------------------------------------------------------------------- strstream *Filler(int n) { int i; strstream *Space; Space = new strstream; for (i = 0; i < n; i++) (*Space) << ' '; (*Space) << '\0'; return Space; } //------------------------------------------------------------------------ // SleeperThread::SleeperThread() // Constructor //------------------------------------------------------------------------ SleeperThread::SleeperThread(int No, int Iteration) { number = No; iteration = Iteration; ThreadName.seekp(0, ios::beg); ThreadName << "SleeperThread" << No << '\0'; } //------------------------------------------------------------------------ // SleeperThread::ThreadFunc() // //------------------------------------------------------------------------ void SleeperThread::ThreadFunc() { Thread::ThreadFunc(); strstream *Space; Space = Filler(this->number); for (int i = 1; i <= iteration; i++) { cout << Space->str() << ThreadName.str() << " is going to work" << endl; Delay(); // go to work Alarm.Slumber(number); // sleep } Exit(); } //------------------------------------------------------------------------ // DriverThread::DriverThread() // Constructor //------------------------------------------------------------------------ DriverThread::DriverThread() { ThreadName.seekp(0, ios::beg); ThreadName << "DriverThread" << '\0'; } //------------------------------------------------------------------------ // DriverThread::ThreadFunc() // //------------------------------------------------------------------------ void DriverThread::ThreadFunc() { Thread::ThreadFunc(); while (1) { Delay(1); // Delay for a fixed amount of time Alarm.ClockTick(); } } // end of IncDec-Thrd.cpp