I'm trying to use semaphores to control how many instances of a job can be run at the same time. While this is fairly simple with a wait method, I also want the value to be configurable at runtime so that I can bump the count up or down.
I realize that there may be issues bumping the count down but is there a way to actually do this? Is this the right approach to be using semaphores?
According to the documentation of ReleaseSemaphore, for the lReleaseCount parameter:
The amount by which the semaphore object's current count is to be increased. The value must be greater than zero. If the specified amount would cause the semaphore's count to exceed the maximum count that was specified when the semaphore was created, the count is not changed and the function returns FALSE.
This, and other documentation suggests that a semaphore is not the right choice for your limitation. A semaphore, once created, has a hard maximum that cannot be changed without recreating the semaphore. In other words, it is not a dynamic value that can be changed.
You need to find another way of managing your limit in this scenario.
One way you could use a semaphore would be to allocate a semaphore that is big enough for all future needs, and then just grab enough "instances" of it to reduce the available number back down to what you need at the moment. When you want to increase the number of available instances, just release some of those you grabbed at the beginning.
However, I question why you want to do this. What is the limiting factor that really decides how many jobs you can execute at the same time here? Most likely a semaphore is not the right answer to this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With