2010年2月5日金曜日

C++マルチスレッド

メンバー関数のマルチスレッドは少し細工が必要なので
プログラム例を載せます。

★Key.h

class Key
{
private:
HANDLE hThreadKanon;
static unsigned int __stdcall ThreadKanon(void *p);
void Kanon();

public:
Key();
~Key();
}

★Key.cpp

Key::Key()
{
hThreadKanon = (HANDLE)_beginthreadex(NULL, 0, &Key::ThreadKanon, this, 0, NULL);
}

Key::~Key()
{
WaitForSingleObject(hThreadKanon, INFINITE);
CloseHandle(hThreadKanon);
}

unsigned int __stdcall Key::ThreadKanon(void *p)
{
Key* pThis = (Key*)p;
pThis->Kanon();
_endthreadex(0);
return 0;
}

void Key::Kanon()
{
//実処理
}

2 件のコメント:

  1. お、久しぶりに記事書いてくれたんですね。
    何の処理なのか分かりませんが今後も
    記事楽しみにしてます^^

    返信削除
  2. プログラムの説明無しとか、ほとんど私的メモですね。
    次は違う話題を(^_^;)

    返信削除