In reply to agkiran:
It’s protected:
void'(m_atomic.try_get(1));
m_atomic.put(1);
Before adding a key to the semaphore, it tries to get a key. A second call to XatomicX(0) results in any existing key getting removed ( back down to 0 keys again ), followed by adding a key ( back up to 1 ).
That’s still racy though. If two threads somehow tried to execute those same 2 lines at the same time, Verilog’s scheduler rules allow both try_gets to be evaluated, followed by the puts. This would result in 2 keys added to the semaphore. In practice I don’t think any simulators actually would do that, but it’s allowed by the LRM.