Shawn Hargreaves Blog
The WinRT programming environment, which is used to create Metro applications for Windows 8, replaces the old Win32 threading APIs such as CreateThread with a new ThreadPool::RunAsync API. This is generally a good thing (ThreadPool is better than CreateThread for several reasons) but it can be a pain when trying to port existing code that depends on those older APIs.
Porting shim to the rescue! This code emulates a subset of the Win32 threading APIs as a wrapper on top of WinRT ThreadPool:
Get it here: ThreadEmulation.zip
I recommend only using this for porting legacy code. When writing new Metro applications, it is better to directly use WinRT ThreadPool.
Alecazam, I don't understand your point here. There is no such thing as a thread handle or thread ID in the Windows Store programming model. All the Win32 APIs that return or accept such values are no longer supported, so these concepts no longer exist.
This emulation code provides an emulation of one method (CreateThread) that does return a 'thread handle', but this is an emulated handle, not a real Win32 thread handle. It works exactly like a real thread handle in that you can block on it using any of the Win32 wait APIs, and it will be signalled as soon as the 'thread' has finished running, and you can CloseHandle on it when you are done using it, but it is not a real thread handle at the Win32 level, any more than this emulated CreateThread method creates a real Win32 thread (actually, the 'thread handle' returned by the CreateThread emulation is a Win32 manual reset event).
This distinction is unimportant, however, because the emulated handle works correctly with the emulated CreateThread, and you do not have access to any of the various Win32 API methods that use real thread handles, so what difference does it make whether the returned value is a 'real' thread handle or not?
Shawn, good stuff.
I'm curious why you don't just use the Fls functions to replace Tls? (eg. FlsAlloc for TlsAlloc). Should be simpler and possibly faster.
I had various routines that would obtain the current thread handle and pull up data off of that. Since your emulation returns the thread back to the pool after the task completes, legacy code does not expect to see the same "thread handle" (emulated or real) appear until CloseHandle is called on the "thread handle". I'm only pointing out that the emulation isn't quite right for legacy code. I know it's an event handle, I use the same approach.
I should clarify that my data was tied to thread id (and that is obtainable in a store app). It's just the thread id of the thread in the thread pool. I could change the legacy code to reference off the thread (event) handle, but I wanted to keep that code unchanged.
My legacy code is using GetThreadID. Is there any way to have the code that is running on a thread know what it's handle is?
Jim, I looked briefly at trying to emulate the thread ID family of APIs, but obviously didn't get as far as actually doing that. My initial thought is this should be possible but will be quite subtle and in the end I just wasn't convinced how necessary it really was. My experience is that most of the things you can do with thread IDs can also be done using thread local storage (in fact, thread ID is most often used just as a building block for some kind of roll-your-own TLS equivalent).
I'm curious what your code is using GetThreadID for? Could this be changed to use TLS instead?
...I am having issues on calling via C++ thread routine such as shown below
where no compile error but p->f_iOnThreadProc() seems not calling.
public ref class MainPage sealed
static DWORD WINAPI fnThread (__in void* pv);
int f_iOnThreadProc (void);
using namespace ThreadEmulation;
DWORD WINAPI App1::MainPage::fnThread (__in void* pv)
p = reinterpret_cast<App1::MainPage^>(pv);
p->f_iOnThreadProc(); // NOTE: No error, but f_iOnThreadProc() is not called.
int App1::MainPage::f_iOnThreadProc (void)
int x = 0;
// lbx is Type Listbox
lbx->Items->Append( "f_iOnThreadProc()...OK" );
TextBlock^ tbx = ref new TextBlock();
while( 1 )
hr = WaitForSingleObjectEx( m_hEvent, 512, TRUE );
if (hr == WAIT_OBJECT_0)
tbx->Text = "cnt=[" + (x).ToString() +"]";
// btnTest is Type Button.
btnTest->Content = tbx;
int App1::MainPage::f_iOnThreadOpen (void)
if( m_hEvent )
m_hEven = CreateEventEx( nullptr, nullptr, CREATE_EVENT_INITIAL_SET, EVENT_ALL_ACCESS);
if( m_hEvent == nullptr )
if( m_hThread )
m_hThread = CreateThread( 0x00,
&m_dwThreadID ); // m_dwThreadID default to 0x00,
// assert(unusedThreadId == nullptr); is remarked under ThreadEmulation.cpp
if (m_hThread == nullptr )
Very nicely done. I have avoided a port to RT for the last 2 years because I have long running threads that occasionally sleep and wait, and the effort to port the 500,000 lines of affected code was too high, given RT's (lack of) market. Just throwing items at a thread pool worked for a number of items, but the long running ones concerned me, especially with thread pool starvation issues. Your use of WorkItemOptions::TimeSliced appears to solve that, although, of course, it requires verification.
This is the first time I have been hopeful about RT in quite a while. I wish I had seen this 2 years ago!
Interesting article. Is it at all possible to say wake up a thread when a metro app is asleep aka all threads are suspended in Win32 API? ResumeThread doesn't seem to actually do anything from an external Win32 application.