日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

事件,信号量,互斥量

發(fā)布時(shí)間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 事件,信号量,互斥量 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

信號量:
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函數(shù)
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; }


總結(jié)

以上是生活随笔為你收集整理的事件,信号量,互斥量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。