使用关键代码段(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天左右.

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

posted on 2008-02-29 21:28  悠然小调  阅读(2979)  评论(0编辑  收藏  举报