Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-08-01 13:07:27


Braddock Gaskill wrote:

> // alternative #3 - pass by value
> // Constructing shared_ptr's is VERY inefficient
> void f3(future f) {
> bool v = f.get(); // no problem, get() is const
> f.set(!v); // cannot do this, set() is not const
> }

The construction of the shared_ptr (if not optimized out when the source is
an rvalue) consists of one atomic increment. Its destruction at the end of f
costs one atomic decrement and one memory barrier.

In contrast, f.get() costs at best one atomic load and one memory barrier,
and at worst blocks for an unspecified amount of time.

f.set very likely costs one user-kernel transition (and a memory barrier),
which is typically much more than a few atomic instructions.

This of course doesn't address your const-correctness question, which
disappears if you make f.set const. :-)

In the next C++ we'll also have

void f5( future && f );

as an option.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net