locked
Whats the replacement for InitializeCriticalSection?

    Question

  • I have a 3rd party lib (in source) that uses InitializeCriticalSection for thread Synchronization. This API is not allowed for Metro apps. Whats the suggested replacement then?
    Tuesday, March 6, 2012 10:46 AM

Answers

  • We are looking at InitializeCriticalSectionEx() as the replacement.  Its two extra arguments are easily dealt with.  I've not tried this, but it looks like it should work.  What could possibly go wrong? :-]


    Bob Mance

    Tuesday, March 6, 2012 5:27 PM

All replies

  • We are looking at InitializeCriticalSectionEx() as the replacement.  Its two extra arguments are easily dealt with.  I've not tried this, but it looks like it should work.  What could possibly go wrong? :-]


    Bob Mance

    Tuesday, March 6, 2012 5:27 PM
  • Good find Bob. The synchronization APIs available to Metro style apps are documented in the System category of Win32 and COM for Metro style apps

    -David

    Tuesday, March 6, 2012 5:36 PM
    Moderator
  • Thanks Bob, I will try it out :)

    @David: Should have looked more careful there :)

    Tuesday, March 6, 2012 5:40 PM
  • Hmmm... and whats the replacement for "Sleep"? I could not find it in the docs. No SleepEx only a "SleepConditionVariableCS".
    Tuesday, March 6, 2012 5:53 PM
  • The WinRT recommended way to accomplish this is by using ThreadPoolTimer.CreateTimer method. A Win32 approach that can be used to simulate Sleep is to create an alertable object (CreateEventEx for example) and then use WaitForSingleObjectEx with the desired timeout.

    You must be cautious not to block the UI thread in your implementation or your app will most likely fail certification. I would encourage you to review from a perspective of using an async pattern to determine if there was a way to accomplish the same task without blocking.


    David Lamb


    Tuesday, March 6, 2012 7:11 PM
    Moderator
  • Thanks David, I have reimplemented Sleep like this in the 3rd party lib:

    void WINAPI Sleep(DWORD timeout) {
      static HANDLE mutex = CreateEvent(0, 0, 0, 0);
      WaitForSingleObjectEx(mutex, timeout, FALSE);
    }

    The AppVerifier is not complaining about CreateEvent but are you saying I should use CreateEventEx instead, just to be sure?

    Its also not complaining about WaitForMultipleObjects although I could not find this method in the Metro API for Win32 docs.

    Since this is a 3rd Party dll I cannot introduce async patterns so easily.

    Wednesday, March 7, 2012 9:30 AM