事件,信号量,互斥量

信号量:
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值