使用关键代码段(Critical Section)的注意事项:
1.尽可能为每一组需要保护的资源设置一个关键代码段;
2.如果一个线程中同时进入了2个或更多的关键代码段,则其他同类线程的进入顺序必须相同,否则可能产生死锁;
3.用 InitializeCriticalSectionAndSpinCount 并设置dwSpinCount 的高位,则会预先分配内核对象的空间——某种程度上避免的EnterCriticalSection函数失败;
If the high-order bit is set, the function preallocates the event used by the EnterCriticalSection function
4.如果不想进程暂停那么试试TryEnterCriticalSection
5.实际上,EnterCriticalSection也会超时,通常是30天左右.

优点:同步速度快
缺点:不能跨进程同步

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2021-10-30
  • 2021-11-04
  • 2021-12-27
  • 2021-06-13
  • 2022-02-18
  • 2021-09-27
猜你喜欢
  • 2022-01-05
  • 2022-12-23
  • 2021-07-26
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案