|
Boost : |
From: Bram Neijt (bneijt_at_[hidden])
Date: 2008-08-29 19:23:22
Dear reader,
I've been wanting to implement a future in C++ for some time, and
combined some code to come up with the attached. The idea is that the
future is a function wrapper around a function call which will call the
function in a second thread upon creation. Then at a later moment, the
future can yield the return value of the wrapped function (or block
until this result is know).
I'm happy with the result I got so far. It's a very rough draft, and I
would like your help to clean up some details:
- I would like to have the Future wrapper work with the boost::function<
int (argument, argument)> syntax, but I can't figure out how to get the
return type from that syntax to allow for the return value to be defined
(the d_result member).
- What would be the best way to handle function arguments? Binding all
the parameters before passing the resulting bound function object seems
like the solution, but is it really the best solution?
And last but not least: has this already been done and what is the
general opinion within the boost community on having a class like this?
Greetings,
Bram Neijt
Attached are two files, one is the Future class and the second is a test
program showing it's general functioning. The output of running the test
program should show:
Creating future
Future thread working
Main waiting for 3 seconds
Future thread working
Future thread working
Calling upon future to return
Future thread working
Future thread working
Future thread working
Future thread working
Future thread working
Future thread working
Future returned: 9
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk