マルチスレッドプログラミング相談室 その6at TECH
マルチスレッドプログラミング相談室 その6 - 暇つぶし2ch746:719
08/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;
 }
};



次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch