Priority Auto-Reset Event

I have this problem: Thread1 can set the auto-reset event, and there are many threads that can wait for the event. Is there a way to indicate the priority of the waiting threads for a particular event, that is, if we say that the event is set, and both A and B are waiting for it, I want to make sure that B will work, and A will wait for the next chance. Any synchronization objects for this? Language is not so important.

Thanks at Advance

+6
c ++ multithreading synchronization c # winapi
source share
2 answers

The problem you are describing requires a real-time scheduler and synchronizer. Although I understand that such things exist in the Windows API, I tried very hard not to know about them.

My recommendation, if the rest of the system is not a real-time system, should minimize your own decision using the priority queue. Make sure that each thread transfers its effective priority to the queue during registration, blocks this thread on condvar and organizes it so that when an event occurs, it is delivered only to the first thread in the queue.

There will probably be a bit of an ugly hack, but if the number of threads is small, you can make some brute force with one conduit in the waiting thread and make it work.

Depending on what you need, consider the following: each recipient event stream has an associated event queue (maybe this is really a mailbox, a queue with 1 depth) with a mutex and a converter. When threads are registered to receive an event, they are added to the priority queue, as described above, and then blocked. When an event occurs, the event delivery thread selects the queue member with the highest priority, deletes it, sends an event notification to a separate thread event queue, waking up the thread.

+3
source share

If thread A has higher priority than thread B, then the scheduler will choose to wake up A, preferring most of the time B. You can use SetThreadPriority to change the priority of the threads.

It is generally believed that it is better to write your own code so that some thread wakes up the same work as before - the most important work, whatever it is. Then the planner can choose any of them that is most convenient for him and perform the same work.

If you have several threads, and you need the event to be handled by a specific thread, then there is an event for the event specifically for this thread.

+1
source share

All Articles