I'm trying to set spin count for CRITICAL_SECTION to zero by different methods:
int main()
{
CRITICAL_SECTION cs;
::InitializeCriticalSection(&cs);
printf("Spin count by default %08X\n", cs.SpinCount);
::DeleteCriticalSection(&cs);
::InitializeCriticalSectionAndSpinCount(&cs, 0);
printf("Spin count with zero spin count init %08X\n", cs.SpinCount );
::DeleteCriticalSection(&cs);
::InitializeCriticalSectionEx(&cs, 0, 0);
printf("Spin count with zero spin count and flags init %08X\n", cs.SpinCount );
::DeleteCriticalSection(&cs);
::InitializeCriticalSection(&cs);
::SetCriticalSectionSpinCount(&cs, 0);
printf("Spin count after explicit reset to zero %08X\n", cs.SpinCount);
::DeleteCriticalSection(&cs);
}
In Windows 7, all results are 0 as expected.
In Windows 10, except the last one, all result in 0x020007D0
value. The last one result in 0x02000000
.
Apparently, 0x07D0
is actual spin count (2000
in decimal), and 0x02000000
is one of these flags:
#define RTL_CRITICAL_SECTION_FLAG_NO_DEBUG_INFO 0x01000000
#define RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN 0x02000000
#define RTL_CRITICAL_SECTION_FLAG_STATIC_INIT 0x04000000
#define RTL_CRITICAL_SECTION_FLAG_RESOURCE_TYPE 0x08000000
#define RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO 0x10000000
I'm afraid that RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
may cause critical section to spin even if I asked it not to spin by using SetCriticalSectionSpinCount
.
Is there any way not to define RTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
by using standard documented APIs ?
SetCriticalSectionSpinCount
also works - it can set spin count to zero. The only problem I have is apparently enforcedRTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
flag. – GomutiTryEnterCriticalSection()
if you don't want to block. Or you could use a mutex. Note thatRTL_CRITICAL_SECTION_FLAG_DYNAMIC_SPIN
is undocumented; there doesn't seem to be any information about what it's actually for. – HallucinatoryTryEnterCriticalSection
is not an option, as I want to block if object is owned. WinAPI mutex is not an option either, as I don't want to go kernel when object is now owned. Closest alternative is Slim Reader/Writer Lock, when locked byAcquireSRWLockExclusive
/ReleaseSRWLockExclusive
, it seem to do exactly what I need, except that it is without recursion support. From C++,boost::mutex
or Visual Studio implementation ofstd::mutex
also behave as I want (and there are even recursive versions of those). Yet, my question is about taking control over Critial Section behavior. – Gomuti