boost mutex

boost mutex

boost mutex

better use boost::shared_mutex!!
=> see boost::shared_mutex

essentials

  • mutable
  • scoped_lock

boost::mutex example

#include "boost/thread/mutex.hpp" /* mutex* */

class Some {
#define _DOBODIESLOCK() \
    boost::mutex::scoped_lock __lk(this->bodiesLock); \
    (void)(__lk)

    Foo getFoo(void) const
    {
        _DOBODIESLOCK();
        ...
        Foo ret = this->foo:
        ...
        return ret;
    }
    void setFoo(Foo const& foo)
    {
        _DOBODIESLOCK();
        ...
        this->foo = foo;
        ...
    }

    Foo foo;
    mutable boost::mutex bodiesLock;/* !! */
#  undef _DOBODIESLOCK
};

boost shared mutex

essentials

  • mutable
  • shared_lock for read
  • upgrade_lock and upgrade_to_unique_lock for write

example

#include "boost/thread/shared_mutex.hpp" /* shared_mutex */

class Some {
#define _DOSHAREDREAD() \
    boost::shared_lock<boost::shared_mutex> __rLock(this->rwlock); \
    (void)(__rLock)
#define _DOEXCLUSIVEWRITE() \
    boost::upgrade_lock<boost::shared_mutex> __uplock(this->rwlock); \
    boost::upgrade_to_unique_lock<boost::shared_mutex> __wLock(__uplock); \
    (void)(__wLock)

    Foo getFoo(void) const
    {
        _DOSHAREDREAD();
        ...
        Foo ret = this->foo:
        ...
        return ret;
    }
    void setFoo(Foo const& foo)
    {
        _DOEXCLUSIVEWRITE();
        ...
        this->foo = foo;
        ...
    }

    Foo foo;
    mutable boost::shared_mutex rwlock;
#  undef _DOSHAREDREAD
#  undef _DOEXCLUSIVEWRITE
}: