что-то никак не пойму где косяк 
*.h
| Код | /*----------------------------------------------------------------------------*/ /* THREAD.H */ /*----------------------------------------------------------------------------*/ #ifndef _THREAD_H_ #define _THREAD_H_ /*----------------------------------------------------------------------------*/ #include <windows.h> #include <process.h> /*----------------------------------------------------------------------------*/ class CallerImpl {
private : CallerImpl (const CallerImpl&); CallerImpl& operator = (const CallerImpl&);
protected : void *arg_;
public : explicit CallerImpl () : arg_ (NULL) {;} virtual ~CallerImpl () {;} virtual unsigned Call(void) const = 0;
}; /*----------------------------------------------------------------------------*/ class StaticCaller : public CallerImpl {
private : typedef unsigned (*Func)(void *); Func func_;
StaticCaller (const StaticCaller&); StaticCaller& operator = (const StaticCaller&);
virtual unsigned Call (void) const { return func_ (arg_); }
public : explicit StaticCaller (Func func, void *arg) : func_(func) { arg_ = arg; } virtual ~StaticCaller () {;}
}; /*----------------------------------------------------------------------------*/ template <class T> class ClassCaller : public CallerImpl {
private : typedef unsigned (T::*Func)(void *arg); T *obj_; Func func_;
ClassCaller (const ClassCaller&); ClassCaller& operator = (const ClassCaller&);
virtual unsigned Call (void) const { return ((obj_->*func_) (arg_)); }
public : explicit ClassCaller (T *obj, Func func, void *arg) : obj_(obj), func_(func) { arg_ = arg; } virtual ~ClassCaller () {;}
};
/*----------------------------------------------------------------------------*/ class Thread {
private: static const unsigned DEFAULT_TERMINATE_ = 100; HANDLE hThread_; unsigned threadID_; volatile bool autoTerminate_; volatile bool isSuspend_;
Thread (const Thread&); Thread& operator = (const Thread&);
static unsigned __stdcall ThreadFunc (void *arg){ Thread *thread = reinterpret_cast<Thread *>(arg); thread->Execute(); _endthreadex (0); return 0; }
bool CreateThread (bool suspend); void DeleteThread (void);
protected: volatile bool terminated_; virtual unsigned Execute(void) = 0; virtual bool OnStart (void); virtual void OnStop (void); virtual bool OnResume (void); virtual bool OnSuspend (void); virtual bool OnTerminate (unsigned waitTime);
public: explicit Thread (bool suspend = true) : hThread_ (INVALID_HANDLE_VALUE), terminated_ (false), isSuspend_ (suspend), autoTerminate_(false) { if (!Thread::CreateThread(suspend)) throw "Ошибка создания потока"; }
virtual ~Thread () { if (!isSuspend_) { if (!autoTerminate_) OnStop(); else DeleteThread(); } else OnTerminate(DEFAULT_TERMINATE_); }
unsigned GetID (void) const { return threadID_; } bool Start (void) { return OnStart (); } bool Resume (void) { return OnResume (); } bool Suspend(void) { return OnSuspend (); } void SetAutoTerminate (bool terminate) { autoTerminate_ = terminate; } bool IsRun (void) const { return (!isSuspend_); }
}; /*----------------------------------------------------------------------------*/ class MethodThread : public Thread {
private: CallerImpl *caller_;
MethodThread (const MethodThread&); MethodThread& operator = (const MethodThread&);
protected: virtual unsigned Execute(void) { return caller_->Call(); }
public: template <class T> explicit MethodThread (T *obj, unsigned (T::*Func)(void *), void *arg = NULL) : caller_(NULL) { caller_ = new ClassCaller <T> (obj, Func, arg); }
explicit MethodThread (unsigned (*Func)(void *), void *arg = NULL) : caller_(NULL) { caller_ = new StaticCaller (Func, arg); }
virtual ~MethodThread () { delete caller_; }
}; /*----------------------------------------------------------------------------*/ #endif /*----------------------------------------------------------------------------*/ |
*.cpp
| Код | /*----------------------------------------------------------------------------*/ /* THREAD.CPP */ /*----------------------------------------------------------------------------*/ #define _THREAD_CPP_ /*----------------------------------------------------------------------------*/ #include "thread.h" /*----------------------------------------------------------------------------*/ bool Thread::CreateThread (bool suspend){
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), 0, TRUE }; hThread_ = (HANDLE)_beginthreadex (&sa, 0, ThreadFunc, this, suspend ? CREATE_SUSPENDED : 0, &threadID_); if (hThread_ == INVALID_HANDLE_VALUE){ return false; }
return true; } /*----------------------------------------------------------------------------*/ void Thread::DeleteThread (void){
__try { if (hThread_ == INVALID_HANDLE_VALUE) return; CloseHandle(hThread_); } __finally { hThread_ = INVALID_HANDLE_VALUE; }
} /*----------------------------------------------------------------------------*/ bool Thread::OnStart (void){
if (!isSuspend_) return true; if (ResumeThread (hThread_) == 0xFFFFFFFF){ return false; }
isSuspend_ = false; return true; } /*----------------------------------------------------------------------------*/ void Thread::OnStop (void){
__try { if (isSuspend_) return; terminated_ = true; WaitForSingleObject (hThread_, INFINITE); } __finally { DeleteThread(); } } /*----------------------------------------------------------------------------*/ bool Thread::OnResume (void) {
if (!isSuspend_) return true; if (ResumeThread (hThread_) == 0xFFFFFFFF){ return false; }
isSuspend_ = false; return true; } /*----------------------------------------------------------------------------*/ bool Thread::OnSuspend (void) {
if (isSuspend_) return true; if (SuspendThread (hThread_) == 0xFFFFFFFF){ return false; }
isSuspend_ = true; return true;
} /*----------------------------------------------------------------------------*/ bool Thread::OnTerminate(unsigned waitTime){
__try { terminated_ = true; if (WaitForSingleObject (hThread_, waitTime) == WAIT_TIMEOUT){ if (!TerminateThread (hThread_, FALSE)){ return false; } } } __finally { DeleteThread(); } return true;
} /*----------------------------------------------------------------------------*/ |
и тогда если
| Код | unsigned Thread (void *arg){ while(1){ Sleep (100); } }
void main(void){ for (int i=0; i < 10; i++){ MethodThread thread (Thread); thread.SetAutoTerminate (true); thread.Start(); } } |
то слетает при втором проходе цикла и мой компилятор говорит, что не освобождены ресурсы.
Где косяк ? |