Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2000-11-20 05:14:16


In message <8v41il+cuh4_at_[hidden]>, William Kempf <sirwillard_at_my-
deja.com> writes
>Your implementation uses the Clone pattern to implement copy and
>assignment. This is what results in your implementation not
>fullfilling requirement #3. My/Jesse's implementation uses a
>reference counting mechanism instead (in fact the working
>implementation I've currently got in Threads uses a hacked version of
>boost::shared_ptr which is thread safe). For efficiency reasons I
>think this is the better implementation, but right now I'm also
>wondering if this might cause some unforseen issues as well.

There are two issues with reference counting of handle/body structures:
(1) thread-safe counting, and (2) thread-safe body access. You have
addressed (1). (2) can only be satisfied if the body is immutable, ie
the execution of operator() is (deep) const, and the body is physically
as well as logically const.

If this requirement is reasonable, then it is the way to go, otherwise
you have to either drop reference counting altogether (efficiency vs
correctness, efficiency loses) or also introduce a const variant of the
function pointer class that can be reference counted, eg
const_function_ptr (implicit vs explicit selection, in this case
implicit loses). This latter separation is the appropriate way to
resolve most problems with reference counting optimisations and threads.

As a point of trivia, there is an interesting alternative solution for
smart pointers templated on their target, which unfortunately
function_ptr is not, and that would be to specialize the template wrt
const, ie function_ptr<T> and function_ptr<const T>. However, there are
two problems with this: (1) it addresses a very different design to what
we are talking about, and (2) it only works on a couple of compilers :->

Kevlin
____________________________________________________________

  Kevlin Henney phone: +44 117 942 2990
  Curbralan Limited mobile: +44 7801 073 508
  mailto:kevlin_at_[hidden] fax: +44 870 052 2289
  http://www.curbralan.com
____________________________________________________________


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk