Multi Threaded Application
Posted on 2000-04-14
I am converting my single threaded application into a Multi threaded app. Everything seems to work ok until I added Mutex's to prevent code accessing the same shared data structures.
What happened was that when a thread created a window, while the other thread was calling WaitSingleObject the whole app would freeze. I found this gem in MSDN
Wait Functions and Creating Windows
You have to be careful when using the wait functions and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses a wait function with no time-out interval, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForSingleObject
I looked at MsgWaitForMultipleObjects but I'm not sure what to do. If MsgWaitForMultipleObjects returns a value indicating a Message Waiting, then what should it do. If I dispatch all the messages waiting, then the program tends to have problems, because of a timing issue (ie the message wasnt intended to be dispatched because some data structures have yet to be initialised by another part of the program.
Does anyone have some sound advice on using these functions?