File: executor.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (207 lines) | stat: -rw-r--r-- 5,917 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2012-2013 Vicente Botet
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/config.hpp>
#if ! defined  BOOST_NO_CXX11_DECLTYPE
#define BOOST_RESULT_OF_USE_DECLTYPE
#endif
#include <iostream>

#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_PROVIDES_EXECUTORS
//#define BOOST_THREAD_USES_LOG
#define BOOST_THREAD_USES_LOG_THREAD_ID
#define BOOST_THREAD_QUEUE_DEPRECATE_OLD

#include <boost/thread/caller_context.hpp>
#include <boost/thread/executors/basic_thread_pool.hpp>
#include <boost/thread/executors/loop_executor.hpp>
#include <boost/thread/executors/serial_executor.hpp>
#include <boost/thread/executors/inline_executor.hpp>
#include <boost/thread/executors/thread_executor.hpp>
#include <boost/thread/executors/executor.hpp>
#include <boost/thread/executors/executor_adaptor.hpp>
#include <boost/thread/executor.hpp>
#include <boost/thread/future.hpp>
#include <boost/assert.hpp>
#include <string>
#include <iostream>
#include <cassert>

boost::future<void> p(boost::future<void> f) {
    assert(f.is_ready());
    return boost::make_ready_future();
}

void p1()
{
  // std::cout << BOOST_CONTEXTOF << std::endl;
  //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
}

void p2()
{
  // std::cout << BOOST_CONTEXTOF << std::endl;
  //boost::this_thread::sleep_for(boost::chrono::seconds(10));
}

int f1()
{
  std::cout << BOOST_CONTEXTOF << std::endl;
  boost::this_thread::sleep_for(boost::chrono::seconds(1));
  std::cout << BOOST_CONTEXTOF << std::endl;
  return 1;
}
int f2(int i)
{
  // std::cout << BOOST_CONTEXTOF << std::endl;
  boost::this_thread::sleep_for(boost::chrono::seconds(2));
  return i + 1;
}

void submit_some(boost::executor& tp)
{
  for (int i = 0; i < 3; ++i) {
    tp.submit(&p2);
  }
  for (int i = 0; i < 3; ++i) {
    tp.submit(&p1);
  }

}


void at_th_entry(boost::basic_thread_pool& )
{

}

int test_executor_adaptor()
{
  std::cout << BOOST_CONTEXTOF << std::endl;
  {
    try
    {
      {
        boost::executor_adaptor < boost::basic_thread_pool > ea(4);
        std::cout << BOOST_CONTEXTOF << std::endl;
        submit_some( ea);
        std::cout << BOOST_CONTEXTOF << std::endl;
#if 1
      // fixme
      // ERROR= tr1::bad_weak_ptr
        {
          boost::future<int> t1 = boost::async(ea, &f1);
          boost::future<int> t2 = boost::async(ea, &f1);
          std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
          std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
        }
        std::cout << BOOST_CONTEXTOF << std::endl;
        submit_some(ea);
        std::cout << BOOST_CONTEXTOF << std::endl;
        {
          boost::basic_thread_pool ea3(1);
          std::cout << BOOST_CONTEXTOF << std::endl;
          boost::future<int> t1 = boost::async(ea3, &f1);
          std::cout << BOOST_CONTEXTOF << std::endl;
          boost::future<int> t2 = boost::async(ea3, &f1);
          std::cout << BOOST_CONTEXTOF << std::endl;
          //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11
          //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98
          std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
          std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
        }
#endif
        std::cout << BOOST_CONTEXTOF << std::endl;
        submit_some(ea);
        std::cout << BOOST_CONTEXTOF << std::endl;
      }
      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::executor_adaptor < boost::loop_executor > ea2;
        submit_some( ea2);
        ea2.underlying_executor().run_queued_closures();
      }
#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::executor_adaptor < boost::basic_thread_pool > ea1(4);
        boost::executor_adaptor < boost::serial_executor > ea2(ea1);
        submit_some(ea2);
      }
#endif
      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::executor_adaptor < boost::inline_executor > ea1;
        submit_some(ea1);
      }
      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::executor_adaptor < boost::thread_executor > ea1;
        submit_some(ea1);
      }
      std::cout << BOOST_CONTEXTOF << std::endl;
#if 1
      // fixme
      // ERROR= tr1::bad_weak_ptr
      {
        boost::basic_thread_pool  ea(4, at_th_entry);
        boost::future<int> t1 = boost::async(ea, &f1);
        std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
      }
#endif
      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::async(&f1);
      }
#if 1
      // fixme
      // ERROR= tr1::bad_weak_ptr

      std::cout << BOOST_CONTEXTOF << std::endl;
      {
        boost::basic_thread_pool  ea(1);
        boost::async(ea,&f1);
      }
#endif
      std::cout << BOOST_CONTEXTOF << std::endl;
      boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
      std::cout << BOOST_CONTEXTOF << std::endl;
    }
    catch (std::exception& ex)
    {
      std::cout << "ERROR= " << ex.what() << "" << std::endl;
      return 1;
    }
    catch (...)
    {
      std::cout << " ERROR= exception thrown" << std::endl;
      return 2;
    }
  }
  // std::cout << BOOST_CONTEXTOF << std::endl;
  return 0;
}


int main()
{
  return test_executor_adaptor();

#if 0 && defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION \
  && defined BOOST_THREAD_PROVIDES_EXECUTORS \
  &&  ! defined BOOST_NO_CXX11_RVALUE_REFERENCES

  boost::basic_thread_pool executor;
  // compiles
  boost::make_ready_future().then(&p);

  // ??
  boost::make_ready_future().then(executor, &p);

  // doesn't compile
  boost::make_ready_future().then(executor, &p);
#endif
}