Number of waiting threads
-
Hello, can I somehow find out how many thread are waiting for an event on WaitForSingleObject (or how many Threads have been released when setting it? Alex
-
Hello, can I somehow find out how many thread are waiting for an event on WaitForSingleObject (or how many Threads have been released when setting it? Alex
AFAIK, no, not with Windows synchronization objects. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
WaitForSingleObject waits for only one object. And to know the state of a thread, use getExitCodeThread.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
Thank you for your answer.
VuNic wrote:
WaitForSingleObject waits for only one object.
But multiple threads can wait for one object - and I would like to know how many. The problem is: when I use a (synchronized) counter before the WaitForSingleObject, how do I know if the Thread has already reached it and is waiting or was interrupted before - I can not unlock the counter atomically with the WaitForSingleObject call... Alex
-
Thank you for your answer.
VuNic wrote:
WaitForSingleObject waits for only one object.
But multiple threads can wait for one object - and I would like to know how many. The problem is: when I use a (synchronized) counter before the WaitForSingleObject, how do I know if the Thread has already reached it and is waiting or was interrupted before - I can not unlock the counter atomically with the WaitForSingleObject call... Alex
LionAM wrote:
how do I know if the Thread has already reached it and is waiting or was interrupted before
There's no way reliably. What thread would be examining this thread to see where it's at? What are you trying to do and how is this counter involved? There may be a simpler solution :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
LionAM wrote:
how do I know if the Thread has already reached it and is waiting or was interrupted before
There's no way reliably. What thread would be examining this thread to see where it's at? What are you trying to do and how is this counter involved? There may be a simpler solution :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I wanted to implement a multiple reader/single writer lock. The counter counts the number of waiting readers (which then wait until the event is set). If a writer unlocks, it sets (or pulses if there are writers waiting) the event so that all waiting readers can resume - but only if they already wait. Alex
-
I wanted to implement a multiple reader/single writer lock. The counter counts the number of waiting readers (which then wait until the event is set). If a writer unlocks, it sets (or pulses if there are writers waiting) the event so that all waiting readers can resume - but only if they already wait. Alex
The lack of an atomic lock/counter-adjust API makes it tricky. There's a ton of solutions on the web (e.g. search "reader writer lock") that vary in performance and whether they favor the reader thread(s) or the writer thread(s). Even if you're looking to design your own, they're worth looking at IMO. Here's an interesting article: Implementing a Reader-Writer lock[^] along with its companion article: Analysis of Reader-Writer lock[^] Here's a simple example - straight forward, if performance isn't a major concern: Reader/Writer Lock[^] Hope that helps a bit, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
The lack of an atomic lock/counter-adjust API makes it tricky. There's a ton of solutions on the web (e.g. search "reader writer lock") that vary in performance and whether they favor the reader thread(s) or the writer thread(s). Even if you're looking to design your own, they're worth looking at IMO. Here's an interesting article: Implementing a Reader-Writer lock[^] along with its companion article: Analysis of Reader-Writer lock[^] Here's a simple example - straight forward, if performance isn't a major concern: Reader/Writer Lock[^] Hope that helps a bit, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I have found a very simple and short solution, using only two "fast critical sections" (interlocked bool) and one (interlocked) counter - and less than 20 lines of code. Alex
Cool! As long as it meets your performance requirements and doesn't deadlock :) Like I mentioned, there's lots of ways to do it - with "big" performance differences between the solutions. Glad you found a solution! Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: