- 746 名前:719 mailto:sage [2008/05/25(日) 16:06:04 ]
- 結局↓のようなクラスにしました。今のところ問題ありません。
class Thread { HANDLE hthread_, hevent_; unsigned int id_; bool cancel_; void run(void) { while (1) { /* 具体的な処理 */ if (cancel_check()) { return; } } } public: Thread(void) : hevent_(CreateEvent(NULL, TRUE, TRUE, NULL)), cancel_(false) {} ~Thread() { CloseHandle(hevent_); } void start(void) { hthread_ = (HANDLE)_beginthreadex(NULL, 0, Thread::entrypoint, this, 0, &id_); } void cancel(void) { cancel_ = true; SetEvent(hevent_); } void suspend(void) { ResetEvent(hevent_); } void resume(void) { SetEvent(hevent_); } bool cancel_check(void) { if (cancel_) { cancel_ = false; return true; } WaitForSingleObject(hevent_, INFINITE); return false; } static unsigned int WINAPI entrypoint(void* instance) { thread_t* p = static_cast<thread_t*>(instance); p->run(); CloseHandle(p->hthread_); p->hthread_ = 0; return 0; } };
|

|