Template Class SemaphoreGuard

Class Documentation

template<typename T>
class SemaphoreGuard

RAII guard for operations with a semaphore. On creation, the semaphore is acquired, and on destruction, it is released.

Note

The usage pattern is slightly different than the normal lock_guard - because acquire() can return false (e.g. if the semaphore is blocked for new acquisitions), the pattern should be the following: `SemaphoreGuard<ReverseSemaphore> guard(sem); if (!guard.acquired()) return;

Template Parameters:

T – Type of the semaphore. Can be any type with methods bool acquire() and void release().

Public Functions

inline explicit SemaphoreGuard(T &semaphore)
inline ~SemaphoreGuard()
inline bool acquired() const

Whether the semaphore acquisition succeeded when constructing this guard.

Note

Always check the result of this function after constructing a guard. The semaphore may be disabled.

Returns:

Success value.