Boost logo

Boost :

From: Frank Mori Hess (fmhess_at_[hidden])
Date: 2008-05-27 20:56:52


It's occurred to me that support for reference counting futures (which
Peter Dimov has suggested a couple times) could be added simply by adding
a method to the promise class

bool promise::has_future() const;

The has_future query would not alter the state of the promise (it wouldn't
put it into some unusable "expired" state). It would simply allow the
promise-setting thread to query if there are any futures and take whatever
action it chooses. So it wouldn't put any burden on someone who wants to
code a fire-and-forget function that returns a future that may be safely
discarded. In a typical implementation, has_future might boil down to a
call of shared_ptr::unique().

This works because threads don't block waiting on promises, only on
futures. When a future's promise is destroyed without ever being
fulfilled, action has to be taken to give the waiting futures an exception
to prevent a thread from blocking forever in vain. But if a promise's
last future disappears, it doesn't need to take any action. All it needs
to do is provide a query that can be used by interested code to detect
that fact.

Also, for debugging a

unsigned promise::future_count() const;

method could be provided, which might be implemented by returning the value
of shared_ptr::use_count minus one.

Any thoughts? These functions could be added independently of whether or
not an explicit future::cancel() is supported.




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