2

What's the correct/best way to communicate from worker thread to the main thread in win32 when working in OOP?

My worker thread runs in a loop, and for certain events including when the thread ends, it needs to tell the main thread, and the main thread do certain things in response.

Currently I am using WM_APP messages from the worker thread to communicate with the main thread. That doesn't look neat though.

StudentX
  • 2,243
  • 6
  • 35
  • 67

3 Answers3

1

If you are comfortable with communicating via Windows Messages, this is perfectly reasonable and fine. It has the benefit of not requiring synchronization. Additional communication can be done via thread-safe objects (that mostly require locking), shared memory, sockets, ... Check well known C++ libraries in their threading sections for possibilities.

Communicating via Windows Messages is one of the simplest ways. This in itself is a value that should not be underestimated and if you do not require platform independence or a form of communication that gives you more possibilities than Windows messages - stick to it.

Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
0

I assume the main thread would be the GUI thread. You can take a look at this SO thread on the similar topic.

Community
  • 1
  • 1
kuskus
  • 176
  • 7
0

Basically there is not standard way of communicating worker thread to main thread. You just concentrate on is your program works fine or not that's it. About threads, A background thread or you can say worker threads are basically used for multitasking purpose means you want to do some thing very heavy like, Reading huge file from disc then you can used thread.

Now one very important thing while using thread is synchronisation of your thread how your are synchronising thread there is lots of issue related to resource allocation and all this first understand how your allocating resource to threads while working.

For more information you may read Using Worker Threads

Santosh Dhanawade
  • 1,756
  • 14
  • 29