user912877
user912877

Reputation: 77

using posthreadmessage() from different threads

i am not quite sure whether this is the correct way to do this (im a hobbyist). I would like to post messages to a worker thread which loops GetMessage() and depending on the message does something. I understand i have to use posthreadmessage() with the handle of that worker thread.

Here is my question:

Can i use

PostThreadMessage(idWorkerThread, WM_COMMAND, (WPARAM)0, (LPARAM)0))

from multiple different threads? I understand that that might cause writes of messages into the Worker threads message queue at the same time from different threads. Is that a problem or is it fine? Essentially i want a N->1 inter-thread communication.

Upvotes: 1

Views: 364

Answers (1)

Pete
Pete

Reputation: 4812

PostThreadMessage is thread safe so you'll be fine. Its a good way of doing it. The other option is to have the thread wait on an event or condition variable and have functions to push events to a queue with appropriate synchronisation.

Upvotes: 1

Related Questions