Back


Download the header File
// Allen Kennedy Jr. // ThreadClass.h class ThreadClass { private: // private because it recasts the 'void*' to 'this' // And it doesn't like it if other objects do nasty things like that static void Running(void * duh); public: void Stop(); virtual void Start(); ThreadClass(); virtual ~ThreadClass(); private: bool go; };
Download the source File
// Allen Kennedy Jr. // ThreadClass.cpp #include "ThreadClass.h" #include <process.h> ThreadClass::ThreadClass() { } ThreadClass::~ThreadClass() { } void ThreadClass::Start() { go = true; _beginthread( ThreadClass::Running, 0, this); } void ThreadClass::Stop() { go = false; } void ThreadClass::Running(void *Mythis) { // recast 'this' pointer from void to ThreadClass so that this static // member function may access private member variables. ThreadClass * InstPtr = (ThreadClass *) Mythis; while(1) { // do something interesting Here // like calculating pi to it's last digit. // or call another member function! // check to see if the thread should stop now if (InstPtr->go == false) break; } // Stop the thread _endthread(); }