I'm calling AfxBeginThread and using CWinThread to spin up a UI thread in my MFC app.
I've noticed that if my main thread tries to PostThreadMessage() to my new thread before the CWinThread::InitInstance() function returns, then PostThreadMessage() will return the error: invalid thread handle.
My guess is that the message pump on the new thread isn't setup until after InitInstance returns. The example code I've seen for AfxBeginThread and the documentation I've read don't do a good job of explaining this behavior, or show a pattern to wait for the thread to be initialized.
What's the best way of blocking my main thread until InitInstance has returned and the thread's message pump is ready to receive messages?