#ifndef THREADS_H #define THREADS_H ///////////////////////////////////////////////////////////////////// // threads.H - Abstract class supporting creation of threads // // ver 1.3 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // // // ///////////////////////////////////////////////////////////////////// /* Module Operations ================= thread is an abstract class that supports the creation of derived thread classes. Each derived thread class must provide a protected run() function that defines the operations that the thread is expected to carry out. The test stub in the accompanying implementation file shows how to define classes derived from thread. Public Interface ================ thread t; // create a thread object t.start(); // create running Win32 thread t.wait(); // wait for thread to complete t.sleep(50); // number of milliseconds to sleep t.canSuspend(false); // set false just before thread acquires lock t.canSuspend(true); // set true just after thread releases lock t.suspend(); // suspend thread operation t.resume(); // resume thread operation HANDLE th = t.handle(); // return Win32 thread handle */ /////////////////////////////////////////////////////////////////////// // Build Process // /////////////////////////////////////////////////////////////////////// // Required Files: // // threads.h, threads.cpp, lockingPtr.h, lockingPtr.cpp, // // sysUtils.h, sysUtils.cpp // // // // Compiler Command: // // cl /GX /DTEST_THREADS threads.cpp lockingPtr.cpp sysUtils.cpp // // // /////////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.3 : 19 Nov 2001 - removed an unused GlobalMutex object from threads.cpp test stub. ver 1.2 : 05 Nov 2001 - added setString() member of derived thread class to demonstrate passing data to thread in type safe way - cosmetic changes to manual, maintenance pages ver 1.1 : 28 Oct 2001 - added missing file names to Build Process comments ver 1.0 : 26 Oct 2001 - first release */ // #include #include #include "lockingPtr.h" #include "sysUtils.h" class thread { public: thread() { } virtual ~thread(); void start(); void wait(); void canSuspend(bool b=true); // set false if thread holds a lock bool suspend(); // otherwise thread may deadlock void resume(); void sleep(int MilliSec); HANDLE handle(); protected: virtual void run()=0; HANDLE th; unsigned int threadID; static unsigned int __stdcall threadProc(void *pSelf); bool _canSuspend; }; inline HANDLE thread::handle() { return th; } inline bool thread::suspend() { if(_canSuspend) ::SuspendThread(th); return _canSuspend; } inline void thread::resume() { ::ResumeThread(th); } // decrement suspend count inline void thread::sleep(int t) { ::Sleep(t); } inline void thread::canSuspend(bool b) { _canSuspend = b; } #endif