#ifndef TMPLTHRDS_H #define TMPLTHRDS_H ///////////////////////////////////////////////////////////////////// // threads.h - generic class supporting creation of threads // // ver 1.2 // // 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 ================= The class thread encapsulates creation of Win32 threads. To construct a thread object, you will first need to create a class that implements your thread's processing in a member with the signature void run(), and pass it to the thread constructor. The test stub in the accompanying implementation file shows how to define these processing classes. Public Interface ================ class myProc { ... } myProc proc; // create a processing object thread t(proc); // 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.0 : 05 Nov 2001 - first release */ // #include #include #include "lockingPtr.h" #include "sysUtils.h" template class thread { public: thread(P p) : _proc(p) { } 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: P _proc; HANDLE th; unsigned int threadID; static unsigned int __stdcall threadProc(void *pSelf); bool _canSuspend; }; template HANDLE thread

::handle() { return th; } template bool thread

::suspend() { if(_canSuspend) ::SuspendThread(th); return _canSuspend; } template void thread

::resume() { ::ResumeThread(th); } // decrement suspend count template void thread

::sleep(int t) { ::Sleep(t); } template void thread

::canSuspend(bool b) { _canSuspend = b; } //----< Win32 thread is created here >------------------------------- template void thread

::start() { th = (HANDLE)_beginthreadex( NULL, // default security attributes 0, // default stack size threadProc, // thread processing function (void*)this, // pointer to this thread object 0, // create thread in running state &threadID // thread identifier assigned by OS ); } //----< destroy thread >--------------------------------------------- template thread

::~thread() { CloseHandle(th); } //----< thread proc wrapper for derived class's run function >------- template unsigned int __stdcall thread

::threadProc(void *pSelf) { ((thread*)pSelf)->_proc.run(); return 0; } template void thread

::wait() { WaitForSingleObject(th,INFINITE); } #endif