Boost logo

Boost :

From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-05-09 08:40:59


Hi,

boost::mutex is a model of Lockable but do not conforms to a SharedLockable,
UpgradeLockable concepts.
Do you think that we can make an adapter of boost::mutex to be a model of
SharedLockable, UpgradeLockable or there is something I've missed?

Consider the following situation:

template <class SharedLockable>
void f(SharedLockable&l) const { /*< externally locked generic const
function>*/
    boost::shared_lock<Lokable> lock(smtx);
    // ... some read operations
 }

 // elsewhere
 boost::mutex mtx; /*< the external user prefer to use a mutex and not a
shared_mutex >*/

 //
 {
     shared_lockable_adapter smtx(mtx); /*< mtx must be adapted to a
shared_mutex >*/
     f(smtx); /*< to call f without compiler errors >*/
     //..
 }

Do you know a simple way to achieve this?
Do you think this adapter could be of general use?

The class shared_lockable_adapter can be defined as follows:

template <typename Lockable>
class shared_lockable_adapter
{
    shared_lockable_adapter(Lockable& mtx): mtx_(mtx) {}
    ~shared_lockable_adapter() {}
    void lock_shared() {mtx_.lock();}
    void unlock_shared() {mtx_.unlock();}
    bool try_lock_shared() { return mtx_.try_lock();}
    void lock() {mtx_.lock();}
    void unlock() {mtx_.unlock();}
    bool try_lock() { return mtx_.try_lock();}
    // other functions ....
private:
    Lockable& mtx_;
};

Comments?
_____________________
Vicente Juan Botet Escriba


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk