I am currently doing the design of a counting semaphore in C++ trying to conform to the same level of standard as Java except for the InterupptedExceptions since C++ std:: threads do not support interruption. I was reading the class documentation in Java and came across two questions that really baffle me at this moment.
https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Semaphore.html
Question 1:
regarding releasing of permits i see that a statement "There is no requirement that a thread that releases a permit must have acquired that permit by calling acquire()" In the implementation of acquire. If there is no ownership concept of acquring a permit and releasing it by same thread in design then what happens in below case.
some rogue thread simply tries to run release and it has never acquired any at all. What will get released then? does it silently return and do nothing?
Question 2:
What happens if a thread acquires a semaphore count and throws an exception where the release is simply missed out perhaps because exception propagates outside of the class of that thread function. Does this lead to permit leak in such case ?