locked
std::thread in metro application

    Question

  • Hi,

    I'm porting some code to a metro application which uses CreateThread a lot, so I've implemented a wrapper to use std::thread as this was reported as OK here:

    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e88b675e-fd16-4567-8231-ae65bf51e286/stdthread-in-metro-style-application?forum=winappswithnativecode

    Whilst this works the native_handle is not something that can be passed to WaitForSingleObjectEx.  Is it possible to get a suitable handle from std::thread?

    This works fine in desktop.

    Thanks.

    Wednesday, October 8, 2014 9:03 AM

Answers

  • Worked around the issue by creating an event which is signalled at thread termination, returning a duplicated handle to the client.
    • Marked as answer by SpecWin Wednesday, October 15, 2014 8:16 AM
    Wednesday, October 15, 2014 8:16 AM

All replies

  • Hallo Wantabeguitarboy,

    I tried the following code and it just worked fine. Under Windows native_handle should return a void*, which is equvalent to the HANDLE parameter in WaitForSingleObject or WaitForSingleObjectEx.

            #include <Windows.h>
    #include <iostream>
    #include <thread>
    
    
    void call()
    {
    	std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    
    int main()
    {
    	std::thread thr(&call);
    
    	WaitForSingleObject(thr.native_handle(), 1000);
    	thr.detach();
    	return 0;
    }

    Viele Grüße
    T. Kahlert
    Entwickler-Hotline für MSDN Online Deutschland

    Disclaimer:
    Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
    Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Schnelle & kompetente Hilfe für Entwickler: kostenfrei!

    Es gelten für die MSDN-Entwickler-Hotline und dieses Posting diese Nutzungsbedingungen , Hinweise zu Markenzeichen, Informationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die MSDN-Entwickler-Hotline .

    Wednesday, October 8, 2014 1:00 PM
  • Hi,

    Thanks for taking a look.  However when I dropped your code into a "Blank Windows Store App" project, inside App::OnLaunched, I get the invalid handle exception in WaitForSingleObjectEx.  Could you try the same?

    I'm using VS2013 update 3.

    Thanks.

    Wednesday, October 8, 2014 1:22 PM
  • Worked around the issue by creating an event which is signalled at thread termination, returning a duplicated handle to the client.
    • Marked as answer by SpecWin Wednesday, October 15, 2014 8:16 AM
    Wednesday, October 15, 2014 8:16 AM