1 | // Copyright (C) 2013 Vicente Botet |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #define BOOST_THREAD_VERSION 4 |
7 | |
8 | #include <iostream> |
9 | #include <functional> |
10 | //#include <future> |
11 | |
12 | #include <boost/thread.hpp> |
13 | #include <boost/shared_ptr.hpp> |
14 | |
15 | int f() |
16 | { |
17 | return 42; |
18 | } |
19 | |
20 | boost::packaged_task<int()>* schedule(boost::function<int ()> const& fn) |
21 | { |
22 | // Normally, the pointer to the packaged task is stored in a queue |
23 | // for execution on a separate thread, and the schedule function |
24 | // would return just a future<T> |
25 | |
26 | boost::function<int ()> copy(fn); |
27 | boost::packaged_task<int()>* result = new boost::packaged_task<int()>(copy); |
28 | return result; |
29 | } |
30 | |
31 | struct MyFunc |
32 | { |
33 | MyFunc(MyFunc const&) = delete; |
34 | MyFunc& operator=(MyFunc const&) = delete; |
35 | MyFunc() {}; |
36 | MyFunc(MyFunc &&) {}; |
37 | MyFunc& operator=(MyFunc &&) { return *this;}; |
38 | void operator()()const {} |
39 | }; |
40 | |
41 | |
42 | int main() |
43 | { |
44 | boost::packaged_task<int()>* p(schedule(fn: f)); |
45 | (*p)(); |
46 | |
47 | boost::future<int> fut = p->get_future(); |
48 | std::cout << "The answer to the ultimate question: "<< fut.get() << std::endl; |
49 | |
50 | { |
51 | boost::function<void()> f; |
52 | MyFunc mf; |
53 | |
54 | boost::packaged_task<void()> t1(f); |
55 | boost::packaged_task<void()> t2(boost::move(t&: mf)); |
56 | } |
57 | |
58 | return 0; |
59 | } |
60 | |
61 | |
62 |