Thread coordination with WaitForSingleObject and CEvent in MFC -
itemprop = "text">
There are many worker formulas in one of my MFC applications, the nature of these threads is as follows:
- Most threads execute their tasks once and consider any condition correct for further execution.
-
In some cases, threads wait infinitely until the situation is correct and in other cases it waits for some time period and based on condition or time Whatever is before before the end of the period, there is some action in it, and then the wait begins.
-
Threads have to be run in the entire life cycle of the application, but it is not necessary that every moment should work
current In each thread, there is an infinite loop, where it works; Since every thread has to work during the lifecycle of the application, I do not want to stop these threads every time and rest again. Inside the loop I have used WaitForSingleObject with an auto-reset sewave for such thread syncing. Seventh objects have been signed from any thread or from UI threads.
In this context, I have the following questions:
i approach is appropriate for my requirement?
ii. Is there a significant overhead for using so many sevent objects for the purpose? Is there a better option?
iii. In some cases, a thread waits for a sevent object infinitely, and the signal only signals from the message message handler for the signal to be received when another message is received from another thread. The message is received through post messages. Here I am concerned about losing the message sent from a thread. If the message handler leaves the message, then it can not keep the position of the civet object and waiting threads have to wait unlimitedly. What precautions should be taken to avoid such a situation? Is there a better way to rebuild the plan?
Please suggest me some better options.
Your attitude is OK Do not worry about many seawwvt objects in your case at least An event should be per thread.
I'm not sure what method you use to get out of the thread, but you may need extra sevent objects to find out if you have to get out of the thread gracefully
In this case, you will use the wait format multibox object in each thread (whether or not to run 1 event, whether or not another event will be to exit the thread). ).
If a lot of threads, I can suggest that whenever you need to, you create child threads. The baby thread will run just once and will get out. In the original thread, you will see again which child thread should be run. You can find the spawn based on the array of event objects. This approach will have less system resources.
Comments
Post a Comment