InitializeCriticalSectionAndSpinCount
The InitializeCriticalSectionAndSpinCount function initializes a critical section object and sets the spin count for the critical section.
BOOL InitializeCriticalSectionAndSpinCount(
LPCRITICAL_SECTION lpCriticalSection,
// pointer to critical section
DWORD dwSpinCount // spin count for critical section
);SetCriticalSectionSpinCountThe SetCriticalSectionSpinCount function sets the spin count for the specified critical section. DWORD SetCriticalSectionSpinCount(
LPCRITICAL_SECTION lpCriticalSection,
// pointer to critical section
DWORD dwSpinCount // spin count for critical section
);
当线程试图进入另一个线程拥有的关键代码段时,调用线程就立即被置于等待状态。这意
味着该线程必须从用户方式转入内核方式(大约1 0 0 0个C P U周期)。这种转换是要付出很大代价的。
因此, InitializeCriticalSectionAndSpinCount 的作用不同于InitializeCriticalSection 之处就在于设置了一个循环锁,不至于使线程立刻被置于等待状态而耗费大量的CPU周期,而在dwSpinCount后才转为内核方式进入等待状态。通常dwSpinCount设为4000较为合适 。