Inter-thread communication (worker threads)

后端 未结 4 573
轻奢々
轻奢々 2021-01-24 13:05

I\'ve created two threads A & B using CreateThread windows API. I\'m trying to send the data from thread A to B.

I know I can use Event object and wait for the Event

4条回答
  •  执笔经年
    2021-01-24 13:57

    First of all, you should keep in mind that Windows provides a number of mechanisms to deal with threading for you: I/O Completion Ports, old thread pools and new thread pools. Depending on what you're doing any of them might be useful for your purposes.

    As to "sending" data from one thread to another, you have a couple of choices. Windows message queues are thread-safe, and a a thread (even if it doesn't have a window) can have a message queue, which you can post messages to using PostThreadMessage.

    I've also posted code for a thread-safe queue in another answer.

    As far as having the thread continue executing, but take note when a change has happened, the typical method is to have it call WaitForSingleObject with a timeout value of 0, then check the return value -- if it's WAIT_OBJECT_0, the Event (or whatever) has been set, so it needs to take note of the change. If it's WAIT_TIMEOUT, there's been no change, and it can continue executing. Either way, WaitForSingleObject returns immediately.

提交回复
热议问题