I'm using a dispatching mechanism in my Win32 application where non-main threads can post a piece of work wrapped in an std::function
for later execution on the main thread. This is realized by creating a hidden window from the main thread, and using its window procedure to filter out these pieces of work, and then executing them. Background threads then use a PostMessage()
call on this window, with a custom message id (>WM_USER), while wrapping a heap-allocated std::function
in the LParam
.
This approach appears to be conceptually sound, but I have some reservations regarding thread-safety. MSDN makes no mention that I found, indicating whether multiple threads may or may not simultaneously post a message to the same window. Is there a definitive verdict?