Using a local semaphore would avoid these undesirable side effects.
The actual decrement dec of said semaphore counter is finally output.