locked
Multi-Threaded synchronization in Metro Apps

    Question

  • What is the proper way to synchronize threads in a C++ metro app? WaitForSingleObject & co don't seem to be accessible.

    The scenario I'm trying to solve is the following. I'm integrating a 3rd party library into C++ Metro app. that library expects to be able to open files synchronously and be able to write to these files right after calling create.

    Since there is only async open file function in WinRT this becomes tricky. The only solution that I see right now is calling async create and spinlocking on a bool variable that gets set in async callback.

    Rewriting said library to accommodate async model is not an option.

    Wednesday, November 02, 2011 4:58 PM

Answers

All replies

  • Do these APIs help you for synchronously creating files (eg CreateFile2)?  http://msdn.microsoft.com/en-us/library/windows/apps/br205753(v=VS.85).aspx
    Thursday, November 03, 2011 12:16 AM
  • What would be the proper way of hooking it up to FilePicker?

    While this may solve the current problem at hand, it would still be great to have a way to sync between threads beyond spinlocking that will suck battery life like crazy.

    Thursday, November 03, 2011 10:16 PM
  • You can use C++11 and its libraries in VS which means that you can use std::promise and std::future. You can set the bool variable in your callback using set_value. In the main thread, you would use future::get() which blocks the thread until the variable has been set. The implementation on Unix/Linux is a wrapper around pthreads. It's probably a wrapper in VS too (around WinAPI) which means that it should be safe and effective.
    Thursday, November 03, 2011 11:08 PM
  • This sample show an example of opening a file using FilePicker, copying it to a temp location and then using some C++ API to synchronously edit it:

    http://code.msdn.microsoft.com/Hybrid-JavaScript-and-C-e6dc77fa

    Thanks


    Raman Sharma | Program Manager, Visual C++ | @rasharm_msft

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)
    Friday, November 04, 2011 1:46 PM
  • @Ghostrider-13

    There are a number of Win32 API's available for event signaling and synchronization listed under the heading of 'Thread and Synchronization' you can still utilize. We still have Events, Critical sections, and the WaitForMultipleObjects, WaitForSigleObject APIs available. Is that what you were looking for?

    http://msdn.microsoft.com/en-us/library/windows/apps/br205762(v=VS.85).aspx

     

    Thanks,
    -David

    Friday, November 04, 2011 11:34 PM
    Moderator
  • David, are you sure that those functions that you list are accessible?  In my application I'm getting the following failure:

    API WaitForSingleObject in kernel32.dll is not supported for this application type. Application1.exe calls this API. 

    That sure doesn't seem to be accessible?

    Thursday, November 17, 2011 5:26 PM
  • WaitForSingleObject is in the Desktop family and is not available.  It is not on the list at Win32 and COM for Metro style apps (system) that David linked to, but WaitForSingleObjectEx is.

    --Rob     

    Thursday, November 17, 2011 6:10 PM
    Owner
  • If you include the Windows 8 SDK version of <windows.h>, CreateThread, WaitForSingleObject and a bunch of other functions are not available to your Metro Style class library / app.

    However, if debug into the c runtime function _beginthread, you will find that it calls CreateThread.  And if you look at the .map file created by the linker, you will see that it links to CreateThread in Kernel32.dll.

    What implications does this have to an app that will eventually be submitted to the Windows Store for approval?  Will it pass certification?  If we re-compile to target ARM devices, will this still work?

    Saturday, March 10, 2012 3:19 AM
  • According to that documentation, I can Enter and Delete a critical section from Metro, but I can't CREATE the CriticalSection object.  Is that an omission, or does it mean we can only interact with critical sections that have been created elsewhere (by Microsoft code and libraries for example?).

    I could really do with a concrete code example of this, as I am trying to use ThreadPoolTimer and an socket reader (which is async), and I want to know how to protect objects that might be manipulated from both callbacks.


    • Edited by ukdiveboy Thursday, June 07, 2012 9:38 PM clarification
    Thursday, June 07, 2012 9:38 PM
  • Condition variables are available in metro have you look at : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682052(v=vs.85).aspx ?
    Friday, June 15, 2012 2:53 AM
  • ukvideboy, you can still use InitializeCriticalSectionEx

    You should however consider using the WRL core wrappers like Microsoft::WRL::Wrappers:CriticalSection so you don't accidentally run into a deadlock because some execution path fails to LeaveCriticalSection.

    Thursday, June 28, 2012 2:35 PM