Win32関連でオサライ(;´Д`) そして、意図したとおりに動作した
// ThreadTest.cpp : コンソール アプリケーション用のエントリ ポイントの定義
//
#include "stdafx.h"
#include "ThreadTest.h"
#include /* _beginthread, _endthread */
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 唯一のアプリケーション オブジェクト
CWinApp theApp;
using namespace std;
VOID threadA(LPVOID param) {
while(1) {
DWORD dwRet = WaitForSingleObject((HANDLE)param, 10000);
if (dwRet == WAIT_TIMEOUT) {
cout << "TIMEOUT\n";
}else{
cout << "GET MUTEX! 3000ms mattari! \n";
Sleep(3000);
ReleaseMutex((HANDLE)param);
}
}
}
VOID threadB(LPVOID param) {
while(1) {
DWORD dwRet = WaitForSingleObject((HANDLE)param, 1000);
if (dwRet == WAIT_TIMEOUT) {
cout << "timeout\n";
}else{
cout << "get mutex! 6000ms mattari! \n";
Sleep(6000);
ReleaseMutex((HANDLE)param);
}
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// MFC の初期化および初期化失敗時のエラーの出力
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 必要に応じてエラー コードを変更してください。
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: この位置にアプリケーションの動作を記述してください。
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
HANDLE mutex = CreateMutex(0, FALSE, NULL);
_beginthread(threadA, 0, (void*)mutex);
_beginthread(threadB, 0, (void*)mutex);
}
while(1);
return nRetCode;
}
Posted by minemaz at 2004年03月10日 19:40