Mfc provides both worker and UI thread. UI thread is enabled with message receiving capabilities (send, post). Could it be possible to let worker thread too receive messages.
Mfc provides both worker and UI thread. UI thread is enabled with message receiving
Share
It seems you need a thread, that can handle multiple messages from another threads. Another threads would add-a-message to the message-queue of this thread. Well, in that case you may use
PeekMessageto startup a loop, which would eventually create a hidden window, and then useGetMessageto get the messages. The other threads would usePostThreadMessagewith the thread ID (the one having Peek/GetMessage), and the message-code,LPARAM,WPARAM.It would be like (not syntactically correct):
The threads would call
PostThreadMessage– See MSDN for more info.When you need to send more data than LPARAM/WPARAM can hold, you eventually need to allocate them on heap, and then delete AFTER processing the message in your custom message-loop. This would be cumbersome and buggy.
But… I would suggest you to have your own class, on top of
std::queue/dequeor other DS, where you can addAddMessage/PushMessage, andPopMessage(or whatever names you like). You need to useSetEvent,WaitForSingleObjectto trigger the new message in loop (See one of the implementation here. You may make it generic for one data-type, or make it template class – that would support any data-type (your underlying DS (queue) would utilize the same data-type). You also need not to worry about heaps and deletions. This is less error prone. You may however, have to handle MT issues.Using Windows events involves kernel mode transition (since events are named/kernel objects), and you may like to use Conditional Variables which are user objects.Or you may straightaway use
unbounded_bufferclass from Concurrency Runtime Library available in VC10. See this article (jump tounbounded_buffer).