| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/thread.hpp>
|
|---|
| 4 | #include <boost/shared_ptr.hpp>
|
|---|
| 5 |
|
|---|
| 6 | int f()
|
|---|
| 7 | {
|
|---|
| 8 | return 42;
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | boost::packaged_task<int>* schedule(boost::function<int ()> const& fn)
|
|---|
| 12 | {
|
|---|
| 13 | // Normally, the pointer to the packaged task is stored in a queue
|
|---|
| 14 | // for execution on a separate thread, and the schedule function
|
|---|
| 15 | // would return just a future<T>
|
|---|
| 16 |
|
|---|
| 17 | boost::function<int ()> copy(fn);
|
|---|
| 18 | boost::packaged_task<int>* result = new boost::packaged_task<int>(copy);
|
|---|
| 19 | return result;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | int main()
|
|---|
| 23 | {
|
|---|
| 24 | boost::packaged_task<int>* p(schedule(f));
|
|---|
| 25 | (*p)();
|
|---|
| 26 |
|
|---|
| 27 | boost::unique_future<int> future = p->get_future();
|
|---|
| 28 | std::cout << "The answer to the ultimate question: " << future.get() << std::endl;
|
|---|
| 29 |
|
|---|
| 30 | return 0;
|
|---|
| 31 | }
|
|---|