|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (C) 2014 Tavendo GmbH |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | +/////////////////////////////////////////////////////////////////////////////// |
| 18 | + |
| 19 | +#include <map> |
| 20 | +#include <memory> |
| 21 | +#include <iostream> |
| 22 | + |
| 23 | +#define BOOST_THREAD_PROVIDES_FUTURE |
| 24 | +#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION |
| 25 | +#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY |
| 26 | +#include <boost/thread/future.hpp> |
| 27 | +#include <boost/asio.hpp> |
| 28 | + |
| 29 | +using namespace std; |
| 30 | +using namespace boost; |
| 31 | + |
| 32 | + |
| 33 | +struct rpcsvc { |
| 34 | + |
| 35 | + rpcsvc (asio::io_service& io) : m_io(io), m_call_id(0) { |
| 36 | + } |
| 37 | + |
| 38 | + struct Call { |
| 39 | + std::shared_ptr<asio::deadline_timer> m_timer; |
| 40 | + promise<float> m_promise; |
| 41 | + }; |
| 42 | + |
| 43 | + future<float> slowsquare(float x, int delay) { |
| 44 | + |
| 45 | + m_call_id += 1; |
| 46 | + m_calls[m_call_id] = Call(); |
| 47 | + |
| 48 | + std::shared_ptr<asio::deadline_timer> |
| 49 | + timer(new asio::deadline_timer(m_io, posix_time::seconds(delay))); |
| 50 | + |
| 51 | + m_calls[m_call_id].m_timer = timer; |
| 52 | + |
| 53 | + int call_id = m_call_id; |
| 54 | + |
| 55 | + timer->async_wait( |
| 56 | + [=](system::error_code ec) { |
| 57 | + if (!ec) { |
| 58 | + cout << m_call_id << " - " << call_id << endl; |
| 59 | + this->m_calls[call_id].m_promise.set_value(x * x); |
| 60 | + this->m_calls.erase(call_id); |
| 61 | + } else { |
| 62 | + cout << "Error in timer: " << ec << endl; |
| 63 | + } |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + return m_calls[m_call_id].m_promise.get_future(); |
| 68 | + } |
| 69 | + |
| 70 | + asio::io_service& m_io; |
| 71 | + int m_call_id; |
| 72 | + map<int, Call> m_calls; |
| 73 | +}; |
| 74 | + |
| 75 | + |
| 76 | +int main () { |
| 77 | + |
| 78 | + try { |
| 79 | + asio::io_service io; |
| 80 | + |
| 81 | + rpcsvc rpc(io); |
| 82 | + |
| 83 | + auto f1 = rpc.slowsquare(2, 2).then([](future<float> f) { |
| 84 | + cout << "call 1 returned" << endl; |
| 85 | + cout << "result 1: " << f.get() << endl; |
| 86 | + }); |
| 87 | + |
| 88 | + auto f2 = rpc.slowsquare(3, 1).then([](future<float> f) { |
| 89 | + cout << "call 2 returned" << endl; |
| 90 | + cout << "result 2: " << f.get() << endl; |
| 91 | + }); |
| 92 | + |
| 93 | + auto f3 = when_all(std::move(f1), std::move(f2)); |
| 94 | + auto f4 = f3.then([](decltype(f3)) { |
| 95 | + cout << "done." << endl; |
| 96 | + }); |
| 97 | + |
| 98 | + io.run(); |
| 99 | + |
| 100 | + f4.get(); |
| 101 | + } |
| 102 | + catch (std::exception& e) { |
| 103 | + cerr << e.what() << endl; |
| 104 | + return 1; |
| 105 | + } |
| 106 | + return 0; |
| 107 | +} |
0 commit comments