Esempio n. 1
0
//!
//! Construct a lock for given semaphore. Wait forever if necessary.
//! numTokens tokens are acquired after the lock construction and
//! will be released when the lock is destructed. Use isOk() to verify
//! proper instantiation (i.e., to verify if tokens were successfully
//! acquired). Locking zero tokens is considered a failure.
//!
Semaphore::Lock::Lock(Semaphore& semaphore, unsigned int numTokens)
{
    numTokens_ = numTokens;
    semaphore_ = 0;
    if (numTokens > 0)
    {
        bool ok = (numTokens == 1)? semaphore.decrement(): semaphore.decrementBy(numTokens);
        if (ok)
        {
            semaphore_ = &semaphore;
        }
    }
}