1. CriticalSection不需要进入内核就可以使用,速度比Mutex快100倍。

2. CriticalSection只能用于同一个进程,而Mutex可以被不同进程使用

 

CriticalSection的伪代码

CRITICAL_SECTION s;

void EnterCriticalSection( CRITICAL_SECTION* s )
{
int spin_count = s.max_count;
while( --spin_count >= 0 )
{
if( InterlockedExchange( &s->Locked, 1 ) == 1 )
{
// we own the lock now
s->OwningThread = GetCurrentThread();
return;
}
}
// lock the mutex and wait for an unlock
WaitForSingleObject( &s->KernelLock, INFINITE );
}

相关文章:

  • 2021-08-09
  • 2022-02-01
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2021-04-13
猜你喜欢
  • 2022-12-23
  • 2019-10-30
  • 2021-12-28
  • 2021-11-01
  • 2021-06-01
  • 2021-06-17
  • 2021-10-11
相关资源
相似解决方案