信号量:
CreateSemaphoreEx
WaitForSingleObject(g_semaphore,INFINITE);
doing...
ReleaseSemaphore(g_semaphore,1,NULL);
mutex
containing a thread ID, it will cach the caller thread id.
WaitForInputIdle used to father process wait child process that have doned all input job.
MsgWaitForMultipleObjects wait both kernel object and window message.
WaitForDebugEvent
SignalObjectAndWait
SetEvent(hDoned)
WaitForSingleObject(hToBeDone,INFINTE); //maybe susppend, so maybe can't get down PlusEvent event.
WaitForSingleObject(hDoned,INFINTE);
PlusEvent(hToBeDone);
SetEvent(hDoned)
WaitForSingleObject(hToBeDone,INFINTE)
should be
SingnalObjectAndWait(hDoned,hToBeDone,INFINITE,FALSE);
使用等待链遍历API来检测死锁 存在局限性,不支持WaitForMultipleObjects函数
OpenThreadWaitChainSession
#include <Windows.h>
#include <process.h>
#include <iostream>
#include <mutex>
HANDLE g_semaphore = NULL;
HANDLE g_event = NULL;
std::mutex g_lock;
unsigned __stdcall ThreadFunc( void* pArguments )
{
do
{
WaitForSingleObject(g_semaphore,INFINITE);
{
std::lock_guard<std::mutex> lockguard(g_lock);
std::wcout << L"Thread: " << (int)pArguments << L" is processing..." <<std::endl;
}
Sleep(500);
ReleaseSemaphore(g_semaphore,1,NULL);
}
while(WaitForSingleObject(g_event,1) == WAIT_TIMEOUT);
{
std::lock_guard<std::mutex> lockguard(g_lock);
std::wcout << L"Thread: " << (int)pArguments << L" ending..." <<std::endl;
}
_endthreadex( 0 );
return 0;
}
int wmain()
{
g_semaphore = CreateSemaphoreEx(NULL,6,6,NULL,0,SEMAPHORE_ALL_ACCESS );
g_event = CreateEventEx(NULL,NULL,CREATE_EVENT_MANUAL_RESET,EVENT_ALL_ACCESS);
HANDLE hThread[6] = { NULL };
for(int i= 0; i< 6; i++)
{
hThread[i] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc, (void*)i, 0, NULL );
}
if( IDYES == MessageBoxW(NULL,L"Do you want to close ?", L"Close", MB_YESNO) )
{
SetEvent(g_event);
}
for(int i=0; i < 6; ++i)
{
CloseHandle( hThread[i] );
hThread[i] = NULL;
}
Sleep(1000);
CloseHandle(g_semaphore);
CloseHandle(g_event);
return 0;
}