Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / thread / example / executor.cpp
1 // Copyright (C) 2012-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 #include <boost/config.hpp>
7 #if ! defined  BOOST_NO_CXX11_DECLTYPE
8 #define BOOST_RESULT_OF_USE_DECLTYPE
9 #endif
10
11 #define BOOST_THREAD_VERSION 4
12 #define BOOST_THREAD_PROVIDES_EXECUTORS
13 //#define BOOST_THREAD_USES_LOG
14 #define BOOST_THREAD_USES_LOG_THREAD_ID
15 #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
16
17 #include <boost/thread/caller_context.hpp>
18 #include <boost/thread/executors/basic_thread_pool.hpp>
19 #include <boost/thread/executors/loop_executor.hpp>
20 #include <boost/thread/executors/serial_executor.hpp>
21 #include <boost/thread/executors/inline_executor.hpp>
22 #include <boost/thread/executors/thread_executor.hpp>
23 #include <boost/thread/executors/executor.hpp>
24 #include <boost/thread/executors/executor_adaptor.hpp>
25 #include <boost/thread/executor.hpp>
26 #include <boost/thread/future.hpp>
27 #include <boost/assert.hpp>
28 #include <string>
29 #include <iostream>
30
31 void p1()
32 {
33   // std::cout << BOOST_CONTEXTOF << std::endl;
34   //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
35 }
36
37 void p2()
38 {
39   // std::cout << BOOST_CONTEXTOF << std::endl;
40   //boost::this_thread::sleep_for(boost::chrono::seconds(10));
41 }
42
43 int f1()
44 {
45   // std::cout << BOOST_CONTEXTOF << std::endl;
46   boost::this_thread::sleep_for(boost::chrono::seconds(1));
47   return 1;
48 }
49 int f2(int i)
50 {
51   // std::cout << BOOST_CONTEXTOF << std::endl;
52   boost::this_thread::sleep_for(boost::chrono::seconds(2));
53   return i + 1;
54 }
55
56 void submit_some(boost::executor& tp)
57 {
58   for (int i = 0; i < 3; ++i) {
59     tp.submit(&p2);
60   }
61   for (int i = 0; i < 3; ++i) {
62     tp.submit(&p1);
63   }
64
65 }
66
67
68 void at_th_entry(boost::basic_thread_pool& )
69 {
70
71 }
72
73 int test_executor_adaptor()
74 {
75   // std::cout << BOOST_CONTEXTOF << std::endl;
76   {
77     try
78     {
79       {
80         boost::executor_adaptor < boost::basic_thread_pool > ea(4);
81         submit_some( ea);
82         {
83           boost::future<int> t1 = boost::async(ea, &f1);
84           boost::future<int> t2 = boost::async(ea, &f1);
85           // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
86           // std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
87         }
88         submit_some(ea);
89         {
90           boost::basic_thread_pool ea3(1);
91           boost::future<int> t1 = boost::async(ea3, &f1);
92           boost::future<int> t2 = boost::async(ea3, &f1);
93           //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11
94           //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98
95           // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
96           // std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
97         }
98         submit_some(ea);
99       }
100       // std::cout << BOOST_CONTEXTOF << std::endl;
101       {
102         boost::executor_adaptor < boost::loop_executor > ea2;
103         submit_some( ea2);
104         ea2.underlying_executor().run_queued_closures();
105       }
106 #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
107       // std::cout << BOOST_CONTEXTOF << std::endl;
108       {
109         boost::executor_adaptor < boost::basic_thread_pool > ea1(4);
110         boost::executor_adaptor < boost::serial_executor > ea2(ea1);
111         submit_some(ea2);
112       }
113 #endif
114       // std::cout << BOOST_CONTEXTOF << std::endl;
115       {
116         boost::executor_adaptor < boost::inline_executor > ea1;
117         submit_some(ea1);
118       }
119       // std::cout << BOOST_CONTEXTOF << std::endl;
120       {
121         boost::executor_adaptor < boost::thread_executor > ea1;
122         submit_some(ea1);
123       }
124       // std::cout << BOOST_CONTEXTOF << std::endl;
125       {
126         boost::basic_thread_pool  ea(4, at_th_entry);
127         boost::future<int> t1 = boost::async(ea, &f1);
128         // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
129       }
130     }
131     catch (std::exception& ex)
132     {
133       std::cout << "ERROR= " << ex.what() << "" << std::endl;
134       return 1;
135     }
136     catch (...)
137     {
138       std::cout << " ERROR= exception thrown" << std::endl;
139       return 2;
140     }
141   }
142   // std::cout << BOOST_CONTEXTOF << std::endl;
143   return 0;
144 }
145
146
147 int main()
148 {
149   return test_executor_adaptor();
150 }