One can use a CRITICAL_SECTION variable to get mutual exclusion.
My question is: does CRITICAL_SECTION support copying? If I pass one by value to another thread, can I know for sure that mutual exclusion will work?
I wouldn't be surprised if the answer is "you cannot do that", but it'd be nice to have some sort of official confirmation. I wasn't able to find a statement either way in the documentation.
No. A CRITICAL_SECTION
cannot be copied. MSDN states this explicitly:
A critical section object cannot be moved or copied.
A quick search through the headers reveals that the structure is defined in winnt.h
, and this definition clearly seems to indicate that copying the structure wouldn't work.
typedef struct _RTL_CRITICAL_SECTION {
PRTL_CRITICAL_SECTION_DEBUG DebugInfo;
//
// The following three fields control entering and exiting the critical
// section for the resource
//
LONG LockCount;
LONG RecursionCount;
HANDLE OwningThread; // from the thread's ClientId->UniqueThread
HANDLE LockSemaphore;
ULONG_PTR SpinCount; // force size on 64-bit systems when packed
} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;
That said, I have no idea why these internal counters are stored in a user-space structure, i.e. what will happen if a program modifies these?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With