
I would like to hear your comments on the idea behind access_monitor template, which ensures (at compile-time) that data, protected by it, can be accessed only with associated lock held. It can be useful when you want to expose all the native interface of your data type (STL container, for example): in such a case, using ordinary monitor pattern is almost impossible. Example usage: typedef access_monitor<data_type, mutex_type> data_access_monitor; void f(data_access_monitor const& d) { data_access_monitor::value_type const* data; data_access_monitor::lock_type lock; boost::tie(data, lock) = d.const_lock(); // do something with data here } Code: #include <boost/thread.hpp> #include <boost/tuple/tuple.hpp> template <typename T, typename Lockable> struct access_monitor { typedef T value_type; typedef boost::unique_lock<Lockable> lock_type; private: typedef boost::detail::thread_move_t<lock_type> lock_move_type; public: boost::tuple<value_type const*, lock_move_type> const_lock() const { lock_type lock(mutex_); return boost::make_tuple(&data_, lock.move()); } boost::tuple<value_type*, lock_move_type> lock() { lock_type lock(mutex_); return boost::make_tuple(&data_, lock.move()); } private: // mutex_ is guaranteed to be locked when you access data_ mutable Lockable mutex_; T data_; }; Sorry for draft-quality code (however, something like the above compiles with MSVC 9). Please CC me, because I am not subscribed. Gregory

Hi, ----- Original Message ----- From: "Gregory Petrosyan" <gregory.petrosyan@gmail.com> To: <boost@lists.boost.org> Sent: Saturday, January 10, 2009 6:19 PM Subject: [boost] [RFC] thread::access_monitor
I would like to hear your comments on the idea behind access_monitor template, which ensures (at compile-time) that data, protected by it, can be accessed only with associated lock held. It can be useful when you want to expose all the native interface of your data type (STL container, for example): in such a case, using ordinary monitor pattern is almost impossible.
I don't know if you know Boost.Synchro (https://svn.boost.org/trac/boost/wiki/LibrariesUnderConstruction#Boost.Synch...). Synchro contains a locking_ptr class that seems to do something similar, but the mutex storage is external to the class.
Example usage:
typedef access_monitor<data_type, mutex_type> data_access_monitor;
void f(data_access_monitor const& d) { data_access_monitor::value_type const* data; data_access_monitor::lock_type lock;
boost::tie(data, lock) = d.const_lock();
// do something with data here }
void f(data_type const& d) { locking_ptr<data_type, mutex_type> data(d,mtx_); // the mytex is locked during the lifetime of data. // do something with data here } This seems more compact, isn't it? If you need to lock several objects with the same mutex at the same time you can use a externally_locked. typedef externally_locked<data_type, mutex_type> data_access_monitor; void f(data_access_monitor const& d1, data_access_monitor const&d1) { strict_locker<mutex_type> guard(mtx); data_type const* data1= d1.get(guard); // data can be obtained if provided a strict_locker data_type const* data2= d2.get(guard); // data can be obtained if provided a strict_locker // do something with data1 and data2 here } What do you think about these classes? Best, Vicente
participants (2)
-
Gregory Petrosyan
-
vicente.botet