Boost logo

Boost :

From: Bronek Kozicki (brok_at_[hidden])
Date: 2003-10-25 07:46:03

On Fri, 24 Oct 2003 23:39:09 +0300, Vygis D. wrote:

> I wrote an early draft of some facility which I called 'synchronized' (some
> kind of equivalent to javaish synchronized), this facility does something

You are invited to look at my "threaded pointer", published at

It's kind of smart pointer. It's using Win32 multithreading API instead
of boost::thread and can be compiled with MSVC71 and GCC (mingw) 3.2.3.

The idea is that pointer object can be safely passed around, without
locking. It contains three subobjects:
* pointer to encapsulated object
* pointer to reference counter
* pointer to critical section object

To access encapsulated (ie. synchronized) object you have to call one of
functions on pointer object:
* Get
* TryGet
* operator->

Each of these will create and return accessor object, which implements
pointer sematics (TryGet can return null pointer accessor, but its
non-blocking; other functions are blocking). This accessor has two
* lock object to current thread (you may not create another accessor,
while at least one accessor object is alive at another thread; you can
create more accessor in single thread)
* expose pointer sematics which can be used to access encapsulated
synchronized object

Because accessor object also offers operator->, if you use this operator
on pointer object it will shortcut through Accessor::operator-> to
encapsulated object.

When object is locked to single thread (ie. at least one accessor object
is alive), you may still freely pass its "threaded pointer" between
different threads. Accessor constructor will block only when you try to
access encapsulated object.

I can imagine similar "threaded pointer" which would benefit from
read/write mutexes. Such improved "threaded pointer" could expose
interface (and implementation) similar to following:

Accessor<T> Get()
  {return Accessor<T> (p_,ref_,plock_,write_access);}
const Accessor<T> Get() const
  {return Accessor<T> (p_,ref_,plock_,read_access);}
const Accessor<T> GetConst() const
  {return Accessor<T> (p_,ref_,plock_,read_access);}

Accessor<T> operator->()
  {return Accessor<T> (p_,ref_,plock_,write_access);}
const Accessor<T> operator->() const
  {return Accessor<T> (p_,ref_,plock_,read_access);}

Accessor<T> TryGet();
const Accessor<T> TryGet() const ;
const Accessor<T> TryGetConst() const;

Kind regards


Boost list run by bdawes at, gregod at, cpdaniel at, john at