How to report an event to multiple threads
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.
How to report an event to multiple threads
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
withnotify_all
fromboost
CONDITION_VARIABLE
withWakeAllConditionsVariable
from WinApiCEvent
from WinApi- using a
mutex
andtry_lock
on the threads signal
andslots
fromboost
- 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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK