|
BDE 4.14.0 Production release
|
Provide a timed semaphore class.
This component defines a portable and efficient thread synchronization primitive. In particular, bslmt::TimedSemaphore is an efficient synchronization primitive that enables sharing of a counted number of resources or exclusive access.
bslmt::TimedSemaphore differs from bslmt::Semaphore in that the former supports a timedWait method, whereas the latter does not. In addition, bslmt::Semaphore has a getValue accessor, whereas bslmt::TimedSemaphore does not. In the case of the timed semaphore, getValue cannot be implemented efficiently on all platforms, so that method is intentionally not provided.
bsls::SystemClockType supplies the enumeration indicating the system clock on which timeouts supplied to other methods should be based. If the clock type indicated at construction is bsls::SystemClockType::e_REALTIME, the absTime argument passed to the timedWait method should be expressed as an absolute offset since 00:00:00 UTC, January 1, 1970 (which matches the epoch used in bsls::SystemTime::now(bsls::SystemClockType::e_REALTIME). If the clock type indicated at construction is bsls::SystemClockType::e_MONOTONIC, the absTime argument passed to the timedWait method should be expressed as an absolute offset since the epoch of this clock (which matches the epoch used in bsls::SystemTime::now(bsls::SystemClockType::e_MONOTONIC).
On platforms that support bsl::chrono, there are constructors that take bsl::chrono-style clocks. If the clock type indicated at construction is bsl::chrono::system_clock, then the results will be the same as if bsls::SystemClockType::e_REALTIME was indicated. If the clock type indicated at construction is bsl::chrono::steady_clock, then the results will be the same as if bsls::SystemClockType::e_MONOTONIC was indicated. Constructing from a user-defined clock is not supported.
This section illustrates intended use of this component.
This example illustrates a very simple queue where potential clients can push integers to a queue, and later retrieve the integer values from the queue in FIFO order. It illustrates two potential uses of semaphores: to enforce exclusive access, and to allow resource sharing. This queue allows clients to set a limit on how long they wait to retrieve values.
Note that the IntQueue constructor increments the count of the semaphore to 1 so that values can be pushed into the queue immediately following construction: