生活随笔
收集整理的這篇文章主要介紹了
事件,信号量,互斥量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
信號量: 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;
}
總結
以上是生活随笔 為你收集整理的事件,信号量,互斥量 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。