test_9303.cpp 6.7 KB

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