資源簡介
程序創建兩個線程,第一個線程沒有消息隊列,主線程嘗試給第一個線程發送一個消息,我們可以看到PostThreadMessage()返回FALSE,程序創建的第二個線程有一個消息隊列,主線程中的PostThreadMessage()返回TRUE,程序由Visual C++ 6.0開發,沒有用到MFC(79KB)

代碼片段和文件信息
//?Q1.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
const?unsigned?int?cuStackSize?=?128?*?1024;
class?CThreadInfo
{
protected:
HANDLE?m_hInitialized;
HANDLE?m_hTerminate;?
public:
CThreadInfo();
~CThreadInfo();
DWORD?WaitForInitialized(DWORD?dwWaitTime?=?5000);
DWORD?WaitForTerminate(DWORD?dwWaitTime?=?5000);
void?SetInitialized();
void?SetTerminate();
};
CThreadInfo::CThreadInfo()
{
//?create?two?events
// no?name?autoreset
// not?initially?owned?default?security
m_hInitialized?=?CreateEvent(NULL?FALSE?FALSE?NULL);
if?(m_hInitialized?!=?NULL)
{
m_hTerminate?=?CreateEvent(NULL?FALSE?FALSE?NULL);
if?(m_hTerminate?==?NULL)
{
CloseHandle(m_hInitialized);
m_hInitialized?=?NULL;
}
}
//?an?error??throw!
if?(m_hInitialized?==?NULL?||?m_hTerminate?==?NULL)
{
printf(“ERROR:?Couldn‘t?create?thread?handles!\n“);
throw?1;
}
}
CThreadInfo::~CThreadInfo()
{
//?if?they?exist?close?them
if?(m_hInitialized?==?NULL)
CloseHandle(m_hInitialized);
if?(m_hTerminate?==?NULL)
CloseHandle(m_hTerminate);
}
DWORD?CThreadInfo::WaitForInitialized(DWORD?dwWaitTime?/*?=?5000?*/)
{
DWORD?dwResult?=?WaitForSingleobject(m_hInitialized?dwWaitTime);
if?(dwWaitTime?!=?0?&&?dwResult?==?WAIT_TIMEOUT)
throw?2;
return?dwResult;
}
DWORD?CThreadInfo::WaitForTerminate(DWORD?dwWaitTime?/*?=?5000?*/)
{
DWORD?dwResult?=?WaitForSingleobject(m_hTerminate?dwWaitTime);
if?(dwWaitTime?!=?0?&&?dwResult?==?WAIT_TIMEOUT)
throw?2;
return?dwResult;
}
void?CThreadInfo::SetInitialized()
{
SetEvent(m_hInitialized);
}
void?CThreadInfo::SetTerminate()
{
SetEvent(m_hTerminate);
}
unsigned?__stdcall?ProcOne(void*?pThreadInfoobject)
{
CThreadInfo*?pInfo?=?(CThreadInfo*)?pThreadInfoobject;
try
{
//?let?our?creator?know?we‘re?initialized
pInfo->SetInitialized();
//?wait?until?we‘re?told?to?terminate
pInfo->WaitForTerminate();
}
catch?(int?n)
{
printf(“First?thread?ERROR:?“);
if?(n?==?2)
printf(“wait?timeout!\n“);
else
printf(“some?other?error\n“);
}
return?0;
}
unsigned?__stdcall?ProcTwo(void*?pThreadInfoobject)
{
CThreadInfo*?pInfo?=?(CThreadInfo*)?pThreadInfoobject;
try
{
//?let?our?creator?know?we‘re?initialized
pInfo->SetInitialized();
MSG?msg;
int?nIdleCount?=?0;
while?(1)
{
if?(PeekMessage(&msg?NULL?0?0?PM_REMOVE))
{
printf(“Thread?Two?Received?a?message!?Looped?%d?times\n“?nIdleCount);
fflush(stdout);
nIdleCount?=?0;
}
else
nIdleCount++;
if?(pInfo->WaitForTerminate(0)?!=?WAIT_TIMEOUT)
break;
}
}
catch?(int?n)
{
printf(“Second?thread?ERROR:?“);
if?(n?==?2)
printf(“wait?timeout!\n“);
else
printf(“some?other?error\n“);
}
return?0;
}
int?main(int?argc?char*?argv[])
{
CThreadInfo*?pInfo?=?NULL;
try
{
//?create?a
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????733??1998-11-17?07:14??StdAfx.h
?????文件?????????289??1998-11-17?06:56??StdAfx.cpp
?????文件?????????527??1998-11-17?06:56??Q1.dsw
?????文件????????4519??1998-11-17?07:32??Q1.cpp
?????文件????????4434??1998-11-17?07:32??Q1.dsp
?????目錄???????????0??1998-11-17?06:56??Debug\
?????文件??????217132??1998-11-17?07:31??Debug\Q1.exe
?????文件????????1530??1998-11-17?07:31??Q1.plg
?????文件???????48640??1998-11-17?07:32??Q1.opt
- 上一篇:跳棋游戲
- 下一篇:二叉排序樹 學生管理系統
評論
共有 條評論