Reputation: 883
I started to use C++11 std::thread
(mingw 4.8) so far so good. I ran into a situation with overlapped I/O where sleepEx
was used to put the thread in an alertable wait state. This worked quite well, until QueueUserAPC
had to be used, which returned an "invalid handle error".
After some searching found out that std::thread
uses the pthread library under Windows.
Is there any way to use windows API calls which expect a thread handle with std::thread
?
Or do I need to stick with Windows threads for overlapped I/O ?
Upvotes: 3
Views: 2756
Reputation: 1439
There is already a native win32 implementation of std::thread and sync primitives, see:
https://github.com/meganz/mingw-std-threads
This is a header-only library and works with any version of MinGW that has proper language support for C++11
Upvotes: 0
Reputation: 76579
To solve your issue, MinGW-w64 winpthreads (the pthreads implementation you are using), just like pthreads-win32, allows you to get the native Win32 thread handle for a pthread:
void * pthread_gethandle (pthread_t t);
Note that this is currently an undocumented function.
The corresponding function in pthreads-win32 is:
HANDLE pthread_getw32threadhandle_np(pthread_t thread);
I'd bet this will make your intermixing of the two work, or at least bring to light some bugs in winpthreads which can be fixed. In the latter case, please report them to MinGW-w64.
If the above returns an invalid handle, your best bet is to ask on the MinGW-w64-public mailing list (subscribe first, otherwise you'll have to wait for manual moderation which is silly).
Upvotes: 5
Reputation: 171303
Is there any way to use windows API calls which expect a thread handle with std::thread ?
No, because the Edit: it is, but indirectly, see rubenvb's answer for how to get the native thread handle from a std::thread
in your MinGW build isn't implemented in terms of thread handles.pthread_t
, and you should be able to use std::thread::native_handle()
to get the pthread_t
.
Noone has implemented the necessary support in GCC for the C++11 thread library to use native Windows threads directly.
I had some ideas for a new thead model that would be implemented in terms of native mutexes and condition variables. That would allow you to call std::thread::native_handle()
to get the underlying thread handle to use with the Windows API.
I got as far as rebuilding GCC with my changes applied, but couldn't test them. There was almost no interest in my suggestions and no offers to help from any MinGW contributors, so as I'm not a Windows user, and working on Windows and building MinGW was so painful and frustrating, I gave up. I should put my changes online somewhere, so that someone with more patience than me can finish the work one day.
Upvotes: 3