1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <boost/function.hpp>
|
---|
5 | #include <boost/make_shared.hpp>
|
---|
6 | #include <boost/shared_ptr.hpp>
|
---|
7 | #include <boost/bind.hpp>
|
---|
8 | #include <boost/asio.hpp>
|
---|
9 | #include <boost/thread.hpp>
|
---|
10 | #include <boost/thread/future.hpp>
|
---|
11 |
|
---|
12 | //#define EXAMPLE_1
|
---|
13 | //#define EXAMPLE_2
|
---|
14 | //#define EXAMPLE_3
|
---|
15 | //#define EXAMPLE_4
|
---|
16 | //#define EXAMPLE_5
|
---|
17 | //#define EXAMPLE_6
|
---|
18 | #define EXAMPLE_7
|
---|
19 |
|
---|
20 | // Test functions
|
---|
21 |
|
---|
22 | int int_no_params()
|
---|
23 | {
|
---|
24 | return 42;
|
---|
25 | }
|
---|
26 |
|
---|
27 | int int_with_params(int i)
|
---|
28 | {
|
---|
29 | return i;
|
---|
30 | }
|
---|
31 |
|
---|
32 | std::string string_no_params()
|
---|
33 | {
|
---|
34 | return std::string("forty two");
|
---|
35 | }
|
---|
36 |
|
---|
37 | std::string string_with_params(std::string ans)
|
---|
38 | {
|
---|
39 | return ans;
|
---|
40 | }
|
---|
41 |
|
---|
42 | int main(int /*argc*/, char ** /*argv[]*/)
|
---|
43 | {
|
---|
44 | std::string ans("forty two");
|
---|
45 |
|
---|
46 | #if defined EXAMPLE_1
|
---|
47 | //! Compiles and produces correct result.
|
---|
48 | {
|
---|
49 | boost::packaged_task<int()> example(int_no_params);
|
---|
50 | boost::future<int> f = example.get_future();
|
---|
51 | boost::thread task(boost::move(example));
|
---|
52 | int answer = f.get();
|
---|
53 | std::cout << "Answer to life and whatnot, in English: " << answer << std::endl;
|
---|
54 | task.join();
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #if defined EXAMPLE_2
|
---|
59 | //! Compiles and produces correct result.
|
---|
60 | {
|
---|
61 | boost::packaged_task<std::string()> example(string_no_params);
|
---|
62 | boost::future<std::string> f = example.get_future();
|
---|
63 | boost::thread task(boost::move(example));
|
---|
64 | std::string answer = f.get();
|
---|
65 | std::cout << "string_no_params: " << answer << std::endl;
|
---|
66 | task.join();
|
---|
67 | }
|
---|
68 |
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #if defined EXAMPLE_3
|
---|
72 | //! Doesn't compile.
|
---|
73 | //! error: variable ‘boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)> example’ has initializer but incomplete type
|
---|
74 |
|
---|
75 | {
|
---|
76 | boost::packaged_task<std::string(std::string&)> example(string_with_params);
|
---|
77 | boost::future<std::string> f = example.get_future();
|
---|
78 | example(ans);
|
---|
79 | std::string answer = f.get();
|
---|
80 | std::cout << "string_with_params: " << answer << std::endl;
|
---|
81 | }
|
---|
82 |
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #if defined EXAMPLE_4
|
---|
86 | //! Doesn't compile.
|
---|
87 | //! error: variable ‘boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)> example’ has initializer but incomplete type
|
---|
88 | {
|
---|
89 | boost::packaged_task<std::string(std::string&)> example(string_with_params);
|
---|
90 | boost::future<std::string> f = example.get_future();
|
---|
91 | boost::thread task(boost::move(example), ans);
|
---|
92 | std::string answer = f.get();
|
---|
93 | std::cout << "string_with_params: " << answer << std::endl;
|
---|
94 | task.join();
|
---|
95 | }
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | #if defined EXAMPLE_5
|
---|
99 | //! Doesn't compile in C++03, C++11 only.
|
---|
100 | //! error: extended initializer lists only available with -std=c++11 or -std=gnu++11 [-Werror]
|
---|
101 | {
|
---|
102 | boost::packaged_task<std::string(std::string&)> example
|
---|
103 | { boost::bind(&string_with_params, ans) };
|
---|
104 | boost::future<std::string> f = example.get_future();
|
---|
105 | boost::thread task(boost::move(example), ans);
|
---|
106 | std::string answer = f.get();
|
---|
107 | std::cout << "string_with_params: " << answer << std::endl;
|
---|
108 | task.join();
|
---|
109 | }
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | #if defined EXAMPLE_6
|
---|
113 | //! Doesn't compile in C++03, C++11 only.
|
---|
114 | // packagedTestTest.cpp:94:43: error: invalid use of incomplete type ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
|
---|
115 | // packagedTestTest.cpp:95:37: error: incomplete type ‘task_t {aka boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>}’ used in nested name specifier
|
---|
116 | // boost/thread/future.hpp:1320:11: error: declaration of ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
|
---|
117 | {
|
---|
118 | typedef boost::packaged_task<std::string(std::string&)> task_t;
|
---|
119 | boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, ans));
|
---|
120 | boost::future<std::string> f = example->get_future();
|
---|
121 | boost::thread task(boost::bind(&task_t::operator(), example));
|
---|
122 | std::string answer = f.get();
|
---|
123 | std::cout << "string_with_params: " << answer << std::endl;
|
---|
124 | task.join();
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | #if defined EXAMPLE_7
|
---|
129 | //! Doesn't compile in C++03, C++11 only.
|
---|
130 | // packagedTestTest.cpp:94:43: error: invalid use of incomplete type ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
|
---|
131 | // packagedTestTest.cpp:95:37: error: incomplete type ‘task_t {aka boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>}’ used in nested name specifier
|
---|
132 | // boost/thread/future.hpp:1320:11: error: declaration of ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
|
---|
133 | {
|
---|
134 | boost::asio::io_service io_service;
|
---|
135 | boost::thread_group threads;
|
---|
136 | boost::asio::io_service::work work(io_service);
|
---|
137 |
|
---|
138 | for (int i = 0; i < 3; ++i)
|
---|
139 | {
|
---|
140 | threads.create_thread(boost::bind(&boost::asio::io_service::run,
|
---|
141 | &io_service));
|
---|
142 | }
|
---|
143 | typedef boost::packaged_task<std::string(std::string&)> task_t;
|
---|
144 | boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, ans));
|
---|
145 | boost::future<std::string> f = example->get_future();
|
---|
146 | io_service.post(boost::bind(&task_t::operator(), example));
|
---|
147 | std::string answer = f.get();
|
---|
148 | std::cout << "string_with_params: " << answer << std::endl;
|
---|
149 | threads.join_all();
|
---|
150 | }
|
---|
151 | #endif
|
---|
152 | return 0;
|
---|
153 | }
|
---|