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. Semaphore Max count.

Semaphore Max count.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 2 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.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    I'm trying to figure out where exactly I would use the Max-Count value of a Semaphore. What's it's purpose? I have been thinking that when the number of processes trying to access the S-object reaches Initial-Count, Max-Count comes into picture. For example, If I had specified 3 as the initial count, 5 as the Max-Count and I'm invoking 3 processes to fill the initial-Count quota. These 3 successfully wait-through(pass-through) the wait-function. Now I execute the 4th process.. since I had mentioned 5 as Max-Count, I guessed it will allow only two more process to get into the block-wait.So It works, I'm able to put 4th and 5th processes into wait. (Though they don't pass the wait function, they just wait). I thought if I execute the 6th process, it would fail and I would get WAIT_FAILED result. But on the contrary, it keeps to wait along the 4th and 5th process. So I conclude my guess is wrong. Can anybody explain a bit on this max-count? Thanks.

    ---------------------------- 286? WOWW!:-O

    S 1 Reply Last reply
    0
    • _ _8086

      I'm trying to figure out where exactly I would use the Max-Count value of a Semaphore. What's it's purpose? I have been thinking that when the number of processes trying to access the S-object reaches Initial-Count, Max-Count comes into picture. For example, If I had specified 3 as the initial count, 5 as the Max-Count and I'm invoking 3 processes to fill the initial-Count quota. These 3 successfully wait-through(pass-through) the wait-function. Now I execute the 4th process.. since I had mentioned 5 as Max-Count, I guessed it will allow only two more process to get into the block-wait.So It works, I'm able to put 4th and 5th processes into wait. (Though they don't pass the wait function, they just wait). I thought if I execute the 6th process, it would fail and I would get WAIT_FAILED result. But on the contrary, it keeps to wait along the 4th and 5th process. So I conclude my guess is wrong. Can anybody explain a bit on this max-count? Thanks.

      ---------------------------- 286? WOWW!:-O

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      In Windows, a semaphore blocks when its count is zero. It starts at lInitialCount (as per the documentation[^]). A successful Wait for the semaphore decreases the count by 1. A ReleaseSemaphore from any thread increases the count by 1. So, if you set the initial count to something less than the max count, if you release the semaphore one or more times before anything waits , it can reach the maximum count. I suppose that lets you have a notional initial occupancy of the semaphore - but you need to be aware of and keep track of that. So - in your case, initial count=3, max count=5, so the semaphore believes two of its 'slots' are filled when it is created. The first three waits on the semaphore succeed. Any waits after that will wait until ReleaseSemaphore is called and the thread can acquire the semaphore.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      _ 1 Reply Last reply
      0
      • S Stuart Dootson

        In Windows, a semaphore blocks when its count is zero. It starts at lInitialCount (as per the documentation[^]). A successful Wait for the semaphore decreases the count by 1. A ReleaseSemaphore from any thread increases the count by 1. So, if you set the initial count to something less than the max count, if you release the semaphore one or more times before anything waits , it can reach the maximum count. I suppose that lets you have a notional initial occupancy of the semaphore - but you need to be aware of and keep track of that. So - in your case, initial count=3, max count=5, so the semaphore believes two of its 'slots' are filled when it is created. The first three waits on the semaphore succeed. Any waits after that will wait until ReleaseSemaphore is called and the thread can acquire the semaphore.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        _ Offline
        _ Offline
        _8086
        wrote on last edited by
        #3

        Stuart Dootson wrote:

        So - in your case, initial count=3, max count=5, so the semaphore believes two of its 'slots' are filled when it is created. The first three waits on the semaphore succeed. Any waits after that will wait until ReleaseSemaphore is called and the thread can acquire the semaphore.

        Is it like at first the limit cap would be 3(As it assumes the other 2 to be filled already) but once we start to release semaphores, the cap would be extended to 5? For example: initially available slots: 3 ; Filled slots :2 ; SemVar=3 S1 Created. Successful wait-through. SemVar-- = 2 S2 Created. Successful wait-through. SemVar-- = 1 S3 Created. Successful wait-through. SemVar-- = 0, becomes 0 and becomes non-signled. S4 Cannot be created. Limit Reached. Keeps Waiting. S1 Released. SemVar++ = 1 (Now will it add 2 more counts here?) so that SemVar becomes 3 . Hence the final limit becomes 5. (2 already in use S2,S3 after we release S1). So as I just said, the currently available slots would be 3. Now S4 gets through. S5 gets through. S6 Gets through. So we have S2,S3,S4,S5,S6 in the ultimate list? Now the semaphore starts to operate on 0 to 5 limits. Rather than the 0 to 3 limit. Did I get you right?

        ---------------------------- 286? WOWW!:-O

        S 1 Reply Last reply
        0
        • _ _8086

          Stuart Dootson wrote:

          So - in your case, initial count=3, max count=5, so the semaphore believes two of its 'slots' are filled when it is created. The first three waits on the semaphore succeed. Any waits after that will wait until ReleaseSemaphore is called and the thread can acquire the semaphore.

          Is it like at first the limit cap would be 3(As it assumes the other 2 to be filled already) but once we start to release semaphores, the cap would be extended to 5? For example: initially available slots: 3 ; Filled slots :2 ; SemVar=3 S1 Created. Successful wait-through. SemVar-- = 2 S2 Created. Successful wait-through. SemVar-- = 1 S3 Created. Successful wait-through. SemVar-- = 0, becomes 0 and becomes non-signled. S4 Cannot be created. Limit Reached. Keeps Waiting. S1 Released. SemVar++ = 1 (Now will it add 2 more counts here?) so that SemVar becomes 3 . Hence the final limit becomes 5. (2 already in use S2,S3 after we release S1). So as I just said, the currently available slots would be 3. Now S4 gets through. S5 gets through. S6 Gets through. So we have S2,S3,S4,S5,S6 in the ultimate list? Now the semaphore starts to operate on 0 to 5 limits. Rather than the 0 to 3 limit. Did I get you right?

          ---------------------------- 286? WOWW!:-O

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          _8086 wrote:

          Is it like at first the limit cap would be 3

          The limit is always 5 - it's just that you start with lInitialCount slots available - the difference between the initial count and the maximum count is the number of used slots when the semaphore is created.

          _8086 wrote:

          S1 Released. SemVar++ = 1 (Now will it add 2 more counts here?)

          No - the difference between the initial count and the maximum count is erased when you release the semaphore in threads that never acquired the semaphore. Let's say we have threads T0 and T1 and some worker threads Tw0-Tw5. Let's say we want to create the semaphore with 5 slots, 2 of which are already reserved for T0 and T1. In that case, we use a maximum count of 5 and an initial count of 3 (5-3=2 slots, for T0 and T1). Now, the effective count for threads Tw0-Tw5 to use is 3 until a thread that hasn't acquired the semaphore releases it. So, if thread T0 calls ReleaseSemaphore (remember, it has never waited on the semaphore), the semaphore's effective count goes up to 4. If thread T1 then calls ReleaseSemaphore, its effective count goes up to 5. Does that make sense?

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          _ 2 Replies Last reply
          0
          • S Stuart Dootson

            _8086 wrote:

            Is it like at first the limit cap would be 3

            The limit is always 5 - it's just that you start with lInitialCount slots available - the difference between the initial count and the maximum count is the number of used slots when the semaphore is created.

            _8086 wrote:

            S1 Released. SemVar++ = 1 (Now will it add 2 more counts here?)

            No - the difference between the initial count and the maximum count is erased when you release the semaphore in threads that never acquired the semaphore. Let's say we have threads T0 and T1 and some worker threads Tw0-Tw5. Let's say we want to create the semaphore with 5 slots, 2 of which are already reserved for T0 and T1. In that case, we use a maximum count of 5 and an initial count of 3 (5-3=2 slots, for T0 and T1). Now, the effective count for threads Tw0-Tw5 to use is 3 until a thread that hasn't acquired the semaphore releases it. So, if thread T0 calls ReleaseSemaphore (remember, it has never waited on the semaphore), the semaphore's effective count goes up to 4. If thread T1 then calls ReleaseSemaphore, its effective count goes up to 5. Does that make sense?

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            _ Offline
            _ Offline
            _8086
            wrote on last edited by
            #5

            Stuart Dootson wrote:

            Does that make sense?

            Truly. Let me test this one now. Thanks dude.

            ---------------------------- 286? WOWW!:-O

            1 Reply Last reply
            0
            • S Stuart Dootson

              _8086 wrote:

              Is it like at first the limit cap would be 3

              The limit is always 5 - it's just that you start with lInitialCount slots available - the difference between the initial count and the maximum count is the number of used slots when the semaphore is created.

              _8086 wrote:

              S1 Released. SemVar++ = 1 (Now will it add 2 more counts here?)

              No - the difference between the initial count and the maximum count is erased when you release the semaphore in threads that never acquired the semaphore. Let's say we have threads T0 and T1 and some worker threads Tw0-Tw5. Let's say we want to create the semaphore with 5 slots, 2 of which are already reserved for T0 and T1. In that case, we use a maximum count of 5 and an initial count of 3 (5-3=2 slots, for T0 and T1). Now, the effective count for threads Tw0-Tw5 to use is 3 until a thread that hasn't acquired the semaphore releases it. So, if thread T0 calls ReleaseSemaphore (remember, it has never waited on the semaphore), the semaphore's effective count goes up to 4. If thread T1 then calls ReleaseSemaphore, its effective count goes up to 5. Does that make sense?

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              _ Offline
              _ Offline
              _8086
              wrote on last edited by
              #6

              Hi Stuart, Let me continue my discussion now. Okay, now as you said, We'd require two threads to release the Semaphore without even acquiring it. Assume we are not doing it. Now the Semaphore variable counter would be 0. What should happen if a new process/thread tries to call CreateSemaphore now? Should I check for GetLastError()? So that I might get "Limit_reached" or something? Before putting the new handle into Wait function? Or I should pass the handle to the wait function straight and try to get check the value returned by it? Your answers have been awesome. Thanks a lot.

              ---------------------------- 286? WOWW!:-O

              S 1 Reply Last reply
              0
              • _ _8086

                Hi Stuart, Let me continue my discussion now. Okay, now as you said, We'd require two threads to release the Semaphore without even acquiring it. Assume we are not doing it. Now the Semaphore variable counter would be 0. What should happen if a new process/thread tries to call CreateSemaphore now? Should I check for GetLastError()? So that I might get "Limit_reached" or something? Before putting the new handle into Wait function? Or I should pass the handle to the wait function straight and try to get check the value returned by it? Your answers have been awesome. Thanks a lot.

                ---------------------------- 286? WOWW!:-O

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                _8086 wrote:

                CreateSemaphore

                Ummm - it'd create a new semaphore? Or are you creating a named semaphore and using the same name as previously (and if that's the case, the initial and maximum counts are ignored)? Anyway - CreateSemaphore doesn't matter - it's the wait function[^] that acquires the semaphore. When the semaphore's count is zero, waiting on the semaphore will cause the wait function to block - that's what the documentation[^] says, anyway. If you use WaitForSingleObject, it will return WAIT_OBJECT_0 if it acquires the semaphore or WAIT_TIMEOUT if the semaphore isn't available (unless you specify a timeout of INFINITE, in which case WaitForSingleObject will never return). The 'Using Semaphore Objects[^]' page may clarify things.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                _ 1 Reply Last reply
                0
                • S Stuart Dootson

                  _8086 wrote:

                  CreateSemaphore

                  Ummm - it'd create a new semaphore? Or are you creating a named semaphore and using the same name as previously (and if that's the case, the initial and maximum counts are ignored)? Anyway - CreateSemaphore doesn't matter - it's the wait function[^] that acquires the semaphore. When the semaphore's count is zero, waiting on the semaphore will cause the wait function to block - that's what the documentation[^] says, anyway. If you use WaitForSingleObject, it will return WAIT_OBJECT_0 if it acquires the semaphore or WAIT_TIMEOUT if the semaphore isn't available (unless you specify a timeout of INFINITE, in which case WaitForSingleObject will never return). The 'Using Semaphore Objects[^]' page may clarify things.

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  _ Offline
                  _ Offline
                  _8086
                  wrote on last edited by
                  #8

                  Stuart Dootson wrote:

                  Ummm - it'd create a new semaphore? Or are you creating a named semaphore and using the same name as previously (and if that's the case, the initial and maximum counts are ignored)?

                  Yes I've been talking about named semaphores across multiple processes :). Also why it init, max counts should be ignored in this case?

                  Stuart Dootson wrote:

                  If you use WaitForSingleObject, it will return WAIT_OBJECT_0 if it acquires the semaphore or WAIT_TIMEOUT if the semaphore isn't available (unless you specify a timeout of INFINITE, in which case WaitForSingleObject will never return).

                  Hmm okay now it's clear. I did have a wrong idea. For example, If the initial count = 3, maxcount =5. I thought 3 would get WAIT_OBJECT_ thing, the next 2 Would calls would "successfully-get-blocked" with the wait function. Any other calls after these wouldn't wait successfully. I thought this will also get through the wait function but with some error set in "last-error".

                  ---------------------------- 286? WOWW!:-O

                  S 1 Reply Last reply
                  0
                  • _ _8086

                    Stuart Dootson wrote:

                    Ummm - it'd create a new semaphore? Or are you creating a named semaphore and using the same name as previously (and if that's the case, the initial and maximum counts are ignored)?

                    Yes I've been talking about named semaphores across multiple processes :). Also why it init, max counts should be ignored in this case?

                    Stuart Dootson wrote:

                    If you use WaitForSingleObject, it will return WAIT_OBJECT_0 if it acquires the semaphore or WAIT_TIMEOUT if the semaphore isn't available (unless you specify a timeout of INFINITE, in which case WaitForSingleObject will never return).

                    Hmm okay now it's clear. I did have a wrong idea. For example, If the initial count = 3, maxcount =5. I thought 3 would get WAIT_OBJECT_ thing, the next 2 Would calls would "successfully-get-blocked" with the wait function. Any other calls after these wouldn't wait successfully. I thought this will also get through the wait function but with some error set in "last-error".

                    ---------------------------- 286? WOWW!:-O

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    _8086 wrote:

                    Also why it init, max counts should be ignored in this case?

                    Because the documentation says so[^] :-) And because it makes a lot of sense for the count parameters to be immutable

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    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