locked
Multithreading in winRT?

    Question

  • My physics engine is multi-threaded and it seems to be working fine under metro. but once I tried to "certify" the app, it failed as it is using "CreateThread" and few others.

    What are the interface for therading under winrt?

    Saturday, September 17, 2011 2:42 PM

Answers

  • #using "Windows.System.Threading.winmd" will pull this in:

     

     
    
    #using "Windows.System.Threading.winmd"
    
    using namespace Windows::System::Threading;
    ...
    
    
    void SomeFunction()
    {
       ThreadPool::RunAsync( ref new WorkItemHandler( [](Windows::Foundation::IAsyncAction^ operation)
       {  
    			// Async code here
       }))->Start();
    }
    
    Tuesday, September 20, 2011 10:35 PM
    Moderator

All replies

  • There is the windows.system.threading[1] namespace that provides you with a thread pool that can be used to offload jobs on other threads.

    [1]http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading

    Saturday, September 17, 2011 2:54 PM
  • Assuming i'm a c++ guy, which header file should I include? <threadpoolwinrt.h> breaks compilation and the docs are not really for c++ developers

     

    Thanks

    Monday, September 19, 2011 9:00 PM
  • #using "Windows.System.Threading.winmd" will pull this in:

     

     
    
    #using "Windows.System.Threading.winmd"
    
    using namespace Windows::System::Threading;
    ...
    
    
    void SomeFunction()
    {
       ThreadPool::RunAsync( ref new WorkItemHandler( [](Windows::Foundation::IAsyncAction^ operation)
       {  
    			// Async code here
       }))->Start();
    }
    
    Tuesday, September 20, 2011 10:35 PM
    Moderator
  • 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:23 AM
  • helpful
    Thursday, September 20, 2012 9:52 AM
  • _beginthread, _beginthreadex, _endthread, etc. are NOT available for Windows Store (aka Metro style) apps. _beginthreadex was left in in RC and prior, but is not available for Windows 8 / VS 2012 RTM.

    The recommended threading model for Windows Store apps is to use the WinRT threadpool APIs or the Parallel Patterns Library. You can also use the C++11 thread mechansms.  Classic POSIX-style threading functions (CreateThread, _beginthread, etc.) are not available to Windows Store apps.

    It is possible to emulate POSIX-style threading using the modern threadpools. See this blog post.

    Thursday, September 20, 2012 10:13 PM