Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OK to copy a CRITICAL_SECTION?

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.

like image 367
redtuna Avatar asked Jul 16 '10 22:07

redtuna


2 Answers

No. A CRITICAL_SECTION cannot be copied. MSDN states this explicitly:

A critical section object cannot be moved or copied.

like image 70
James McNellis Avatar answered Oct 16 '22 08:10

James McNellis


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?

like image 25
casablanca Avatar answered Oct 16 '22 09:10

casablanca