Synchronize Pointer (thread safe wrapper)

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 Or you can download it from the boost sandbox. It's listed as sync_ptr.zip The following link has the source code for a test case application: http://code.axter.com/TestCaseAppforThreadSafeObject_sourcecode.zip

Is there any interest in adding to the boost library a synchronized
Axter wrote: 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

Is there any interest in adding to the boost library a synchronized
Axter wrote: 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 ;-)
That's a very good method too. However, it doesn't have the extra logic so that it can be used with an implicit lock. The sync_ptr class can lock implicitly like the following: my_vector->push_back(123); Or it can lock explicitly: sync_ptr<vector<int> >::RefLock v = my_vector.get_locked_obj(); v->push_back(123); The implicit lock is good for single line access. The explicit lock is a better choice when accessing the object multiple lines of code, in which one call depends on the state of another call. sync_ptr<vector<int> >::RefLock v = my_vector.get_locked_obj(); if (v->size() > 0) v->pop_back(); Your monitor class could easily be modified to use the implicit lock. The sync_ptr method might be a little easier to use with abstract pointers. Also, the syn_ptr.h header has a sync_ref_ptr class, which is a reference counting pointer. I think it would be difficult to add reference counting to the monitor class. However, since you're actually storing a concrete type in the monitor class, it might not be all that important to need the reference counting feature. The sync_ptr class also uses a template for the mutex class, so you can use different types of synchroniztion methods (critical-section, mutex, atomic, boost::mutex, etc...). I propose both methods be included. I like to add a modified version of your method to the sync_ptr header, and call it sync_obj if you don't mind.
participants (2)
-
Axter
-
Rene Rivera