3

How to report an event to multiple threads

 2 years ago
source link: https://www.codesd.com/item/how-to-report-an-event-to-multiple-threads.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to report an event to multiple threads

advertisements

How can I send an event to several running threads of my application?

For example: My main thread wants to signal all running threads that the application will exit.

I'm a little confused which of those possibilities leads to an easy and reliable solution:

  • condition_variable with notify_all from boost
  • CONDITION_VARIABLE with WakeAllConditionsVariable from WinApi
  • CEvent from WinApi
  • using a mutex and try_lock on the threads
  • signal and slots from boost
  • or even a global variable with a CriticalSection for protection of getter/setters
  • insert your solution here...

Could you point a beginner into the right direction and perhaps loose some words about the possiblities above I provided.

Added: - OS is Windows XP


Given you want to "signal all running threads that the application will exit" a manual reset even would do the trick.

You need to have each thread check the event occasionally and exit if the event has been signalled.

You then simply signal the event and wait for the threads to complete if you need to.

It's more complex if you need to do this more than once as a manual reset event must be reset manually and you'd need more code to make sure that every thread has received the notification before you reset the event for use in subsequent notifications, but given your question, this will work just fine.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK