"Dinkum/threads/condition"


Include the header "Dinkum/threads/condition" to define the condition variable class condition.

namespace Dinkum {
    namespace threads {
    class condition;
    template<class Lock>
        void notify_all_at_thread_exit(condition& cond, Lock& lock);
    }  // namespace threads
}  // namespace Dinkum

condition


condition · ~condition · notify_one · notify_all · wait · timed_wait


class condition
    {
public:
    condition();
    ~condition();

    template <class Lock>
        void wait(Lock& lock);
    template <class Lock, class Pred>
        void wait(Lock& lock, Pred pred);

    template <class Lock>
        bool timed_wait(Lock& lock, const xtime& xt);
    template <class Lock, class Pred>
        bool timed_wait(Lock& lock, const xtime& xt, Pred pred);

    void notify_one();
    void notify_all();

    // exposition only
private:

    // not implemented
    condition(const condition&);
    condition& operator=(const condition&);
    };

The class describes an object that manages a condition variable. Use wait or timed_wait to wait for a condition to become true. Use notify_one to notify one waiting thread that the condition has become true and notify_all to notify all waiting threads that the condition has become true. Objects of class condition cannot be copied.

condition::condition

condition();

The constructor constructs a condition object; a thread that calls wait on a newly constructed condition object will block.

condition::~condition

~condition();

Precondition: no threads are blocked waiting for the condition object.

The destructor releases any resources used by the condition variable cond.

condition::notify_one

void notify_one();

The member function unblocks one of the threads that is blocked on the condition object at the time of the call. If no threads are blocked on the object at the time of the call the function does nothing.

condition::notify_all

void notify_all();

The member function unblocks all of the threads that are blocked on the condition object at the time of the call. If no threads are blocked on the object at the time of the call the function does nothing.

condition::timed_wait

template <class Lock>
    bool timed_wait(Lock& lock, const xtime& xt);
template <class Lock, class Pred>
    bool timed_wait(Lock& lock, const xtime& xt, Pred pred);

Throws: an object of class lock_error if the lock object lock is not locked.

The first member function atomically unlocks lock and blocks until the condition object is signaled by a call to notify_one or to notify_all, or until after the time specified by the xtime object xt. When the calling thread becomes unblocked it locks the lock object lock before it returns. The function returns false if it returned because the time xt had passed, otherwise it returns true.

The second member function atomically unlocks lock and blocks until the condition object is signaled by a call to notify_one or to notify_all and the predicate pred() returns true (that is, the function incorporates the loop that is needed to avoid spurious wakeups), or until after the time specified by the xtime object xt. When the calling thread becomes unblocked it locks the lock object lock before it returns. The function returns false if it returned because the time xt had passed, otherwise it returns true.

condition::wait

template <class Lock>
    void wait(Lock& lock);
template <class Lock, class Pred>
    void wait(Lock& lock, Pred pred);

Throws: an object of class lock_error if the lock object lock is not locked.

The first member function atomically unlocks lock and blocks until the condition object is signaled by a call to notify_one or to notify_all. When the calling thread becomes unblocked it locks the lock object lock before it returns.

The second member function atomically unlocks lock and blocks until the condition object is signaled by a call to notify_one or to notify_all and the predicate pred() returns true (that is, the function incorporates the loop that is needed to avoid spurious wakeups). When the calling thread becomes unblocked it locks the lock object lock before it returns.

notify_all_at_thread_exit

template<class Lock>
    void notify_all_at_thread_exit(condition& cond, Lock& lock);

Throws: an object of class lock_error if the lock object lock is not locked.

The function registers the condition object cond so that all threads that are blocked on cond will be unblocked by a call to cond.notify_all() when the current thread terminates, after all of the destructors for the current thread's thread-specific storage have been run. The function does not unlock lock, but lock will be unlocked immediately after the subsequent call to cond.notify_all().


See also the Table of Contents and the Index.

Copyright © 1992-2013 by Dinkumware, Ltd. Portions derived from work copyright © 2001 by William E. Kempf. All rights reserved.