Critical Section structure RRS feed

  • Question

  • Hi

    I am intending to use critical section in my application (platform WinCE R3).

    From what I have read on MSDN documentation, it looks like we just call InitializeCriticalSection, but never actually have to populate the individual members of this structure. Is this correct? Is it purely for kernel use and we just pass pointer to it in the related calls (initialise, enter, leave, delete)?



    Tuesday, April 19, 2011 6:11 PM

All replies

  • Your understanding is correct - your code does not need to access the individual fields of the CRITICAL_SECTION structure, aside from calling InitializeCriticalSection.  InitializeCriticalSection will populate the structure with the necessary initial values and the Enter/LeaveCriticalSection will modify them as needed.
    Tom Gensel PTG Systems, LLC
    Tuesday, April 19, 2011 6:33 PM
  • Just to add my 2 cents: there's another useful function related to critical sections that you do not mention, i.e. TryEnterCriticalSection ( If it can be useful, the implementation of the critical section related API is in %_WINCEROOT%\PRIVATE\WINCEOS\COREOS\CORE\DLL\cscode.c, provided that installed the shared source code

    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom Check my blog:
    Wednesday, April 20, 2011 7:47 AM
  • Thanks guys for your replies.

    It also looks like that CS based implementation does not offer a feature timeout if waiting to receive ownership (like the timeout we can specify in WaitForSingleObject() if we were waiting to obtain ownership of a mutex)? Is there anyway to add such a functionality?

    I suppose, I could create some sort of timer and then keep looping in a function calling TryEnterCriticalSection but that is quite time intensive ... ??


    Wednesday, April 20, 2011 5:44 PM
  • Yes, in pseudo code (not tested):
    #define TIMEOUT 10000 // 10 sec timeout
    BOOL bGotCS = FALSE;
    for (int i=0; i<TIMEOUT/100; i++)
    if (TryEnterCriticalSection(&cs))
    bGotCS = TRUE;
    if (bGotCS)

    Good luck,

    Michel Verhagen, eMVP
    Check out my blog:

    Microsoft Embedded Partner
    Consultancy, training and development services.

    Thursday, April 21, 2011 12:52 AM