I’m looking for a way with no side effects.
Ideally, the following code would do the trick:
long currentCount = 0;
::ReleaseSemaphore(h, 0, ¤tCount);
But unfortunately 0 is not allowed as the value of lReleaseCount, so the call returns FALSE.
If you want that value for external monitoring (as you suggest in your comment) then either use the previous value after a call to
ReleaseSemaphore()or IMHO a better solution is that you implement your own ‘interlocked’ counter in addition to your semaphore; you then have your monitoring count and can access it in any way you like… Just don’t use it as a way of seeing if you can ‘enter’ the semaphore…As Chris rightly says, you can’t obtain the current count as it is potentially always changing.