|
楼主 |
发表于 2007-3-25 11:42:48
|
显示全部楼层
回复: 用Win32SDK并发线程的问题。。。。。。。。
REALDB_API void ThreadInit(THREAD* lpThread,UINT (*m_pfThreadProc)(LPVOID),
LPVOID m_lpParameter,UINT nExitTimeLimit,char* pszThreadName)
{
ASSERT (lpThread != NULL);
lpThread->m_pfThreadProc = m_pfThreadProc;
lpThread->m_lpParameter = m_lpParameter;
if (nExitTimeLimit >= 10)
lpThread->m_nExitTimeLimit = nExitTimeLimit;
else
lpThread->m_nExitTimeLimit = THREAD_EXIT_LIMIT;
strcpy(lpThread->m_pszName ,pszThreadName);
lpThread->m_nThreadStatus = THREAD_NONE;
lpThread->m_hThread = NULL;
}
DWORD WINAPI ThreadRun(LPVOID lpParameter)
{
THREAD* lpThread = (THREAD*)lpParameter;
lpThread->m_nThreadStatus = THREAD_RUNNING;
while (TRUE)
{
if (lpThread->m_pfThreadProc == NULL ||
lpThread->m_nThreadStatus == THREAD_EXIT)
break;
else
{
(*(lpThread->m_pfThreadProc))(lpThread->m_lpParameter);
}
Sleep(THREAD_PERIOD);
}
lpThread->m_nThreadStatus = THREAD_EXITED;
lpThread->m_hThread = NULL;
return TRUE;
}
REALDB_API BOOL ThreadBegin(THREAD* lpThread)
{
ASSERT (lpThread != NULL);
DWORD dwThreadID;
lpThread->m_hThread = CreateThread(NULL,0,ThreadRun,
lpThread,0,&dwThreadID);
ASSERT (lpThread->m_hThread != NULL);
return TRUE;
}
REALDB_API BOOL ThreadExit(THREAD* lpThread)
{
ASSERT (lpThread != NULL);
if (lpThread->m_hThread != NULL && lpThread->m_nThreadStatus == THREAD_RUNNING)
{
lpThread->m_nThreadStatus = THREAD_EXIT;
UINT nTimePassed = 0;
while (lpThread->m_nThreadStatus != THREAD_EXITED)
{
Sleep (THREAD_PERIOD);
nTimePassed += THREAD_PERIOD;
if (nTimePassed >= lpThread->m_nExitTimeLimit )
{
LOG ("Error: %s退出超时!",lpThread->m_pszName );
ASSERT (FALSE);
break; //超时了就不等线成退出了
}
}
LOG ("Message: %s退出!",lpThread->m_pszName );
lpThread->m_nThreadStatus = THREAD_EXITED;
lpThread->m_hThread = NULL;
}
return TRUE;
} |
|