Inter-thread communication (worker threads)

后端 未结 4 570
轻奢々
轻奢々 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 14:04

    Since the two threads are in the same process (at least that's what it sounds like), then it is not necessary to "send" data. They can share it (e.g., a simple global variable). You do need to synchronize access to it via either an event, semaphore, mutex, etc.

    Depending on what you are doing, it can be very simple.

    Thread1Func() {
      Set some global data
      Signal semaphore to indicate it is available
    }
    
    Thread2Func() {
      WaitForSingleObject to check/wait if data is available
      use the data
    }
    

提交回复
热议问题