Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Number of waiting threads

Number of waiting threads

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LionAM
    wrote on last edited by
    #1

    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

    E M 2 Replies Last reply
    0
    • L LionAM

      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

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      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[^]

      L 1 Reply Last reply
      0
      • L LionAM

        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

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        AFAIK, no, not with Windows synchronization objects. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • E Eytukan

          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[^]

          L Offline
          L Offline
          LionAM
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • L LionAM

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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:

            L 1 Reply Last reply
            0
            • M Mark Salsbery

              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:

              L Offline
              L Offline
              LionAM
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • L LionAM

                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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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:

                L 1 Reply Last reply
                0
                • M Mark Salsbery

                  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:

                  L Offline
                  L Offline
                  LionAM
                  wrote on last edited by
                  #8

                  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

                  M 1 Reply Last reply
                  0
                  • L LionAM

                    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

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups