C++ Boost

Boost.Threads

basic_lock


This template class defines a generic lock concept type. The mutex, try_mutex, timed_mutex, recursive_mutex, recursive_try_mutex and recursive_timed_mutex all use this template to define their lock types.

Like all the Boost.Threads lock models, the basic_lock is meant to be short lived and is not thread safe, so should not be shared between threads.

Header

#include <boost/thread/xlock.hpp>
    This header is usually not included directly by programmers.

Public Interface


    template <typename M>
    class basic_lock : private boost::noncopyable
    {
    public:
        typedef M mutex_type;
        
        explicit basic_lock(M& mx, bool lock_it=true);
        ~basic_lock();
        
        void lock();
        void unlock();
        
        operator const void*() const;
    };

Constructor

    explicit basic_lock(M& mx, bool lock_it=true);

Constructs a basic_lock and if lock_it is true then calls lock.

Destructor

    ~basic_lock();

Destructs the basic_lock and if locked calls unlock.

lock

    void lock();

Locks the associated mutex model. If the basic_lock is already locked then a lock_error is thrown. Depending on the locking strategy of the mutex model if the calling thread already owns a lock through another lock model this may cause a deadlock or for a lock_error to be thrown.

unlock

    void unlock();

Unlocks the associated mutex model. If the basic_lock is not already locked then a lock_error is thrown.

const void* Conversion

    operator const void*() const;

Implicitly converts the lock to a value that can be used in boolean expressions to test if the lock is currently locked or not.

Example Usage

See the example given in the documentation for the mutex class.


Copyright William E. Kempf 2001 all rights reserved.