|
Boost : |
From: Rene Rivera (grafik.list_at_[hidden])
Date: 2005-05-29 11:21:10
Axter wrote:
> Is there any interest in adding to the boost library a synchronized
pointer class which is a thread safe wrapper class for an object instance.
> If so, please examine the following source code, and please post feedback.
> http://code.axter.com/sync_ptr.h
> http://code.axter.com/sync_ctrl.h
I have a similar utility I use (see attached). But it rather shorter,
code wise. How is your's different from the simpler approach of:
monitor< std::vector<int> > my_vector;
{
monitor< std::vector<int> >::scoped_lock(my_vector) v;
v->push_back(1);
std::cout << (*v)[0] << '\n';
}
??
Obviously I'd be interested in seeing something like this in Boost ;-)
-- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org
/*
Copyright (c) 2003 Redshift Software, Inc.
All Rights Reserved.
*/
#ifndef com_redshift_software_projects_thot_product_base_monitor_h
#define com_redshift_software_projects_thot_product_base_monitor_h
#include <boost/thread/mutex.hpp>
#include <com/redshift-software/libraries/base/System.h>
#include <com/redshift-software/libraries/base/Types.h>
#include <com/redshift-software/projects/Thot/product/Packages.h>
com_redshift_software_projects_thot_product_p {
template <class T> struct monitor
{
typedef boost::mutex mutex_type;
typedef T value_type;
friend class scoped_lock;
class scoped_lock
{
mutex_type::scoped_lock mLock;
value_type & mSlot;
public:
scoped_lock(monitor<value_type> & m) : mLock(m.mMutex), mSlot(m.mSlot) { }
~scoped_lock() { }
inline value_type & operator*() const { return mSlot; }
inline value_type * operator->() const { return &mSlot; }
};
private:
mutex_type mMutex;
value_type mSlot;
};
} p_com_redshift_software_projects_thot_product
#endif
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk