4

如何从中捕获返回值boost::asio::io_service?是否可以使用一些不涉及重写函数的绑定或任何简单的构造?

以下是一个最小的示例。我正在尝试捕获以下值的返回值GetSum()

#include <iostream>
#include <boost/asio.hpp>
#include <functional>

using namespace std;

void SayHello()
{
    std::cout<<"Hello!"<<std::endl;
}

template <typename T>
T GetSum(T a, T b)
{
    std::cout<<"Adding " << a << " and " << b << std::endl;
    return a+b;
}

int main(int argc, char *argv[])
{
    boost::asio::io_service ioservice;

    ioservice.post(&SayHello);
    ioservice.post(std::bind(&GetSum<double>,1,2));

    ioservice.run();
    return 0;
}

为什么?因为我正在设计一个线程池,并且我正在考虑我的选项,以使用户能够获得他的函数的返回值,而不必手动将他的函数与另一个函数包装起来,该函数将为他捕获返回值.

我的解决方案:

int main(int argc, char *argv[])
{
    boost::asio::io_service ioservice;

    ioservice.post(&SayHello);
    double sum;
    ioservice.post([&sum]()
    {
        sum = GetSum(1,2);
    });

    ioservice.run();
    std::cout<< sum <<std::endl; //is 3
    return 0;
}

但我仍然希望有一些更简单的绑定,或者其他东西。

4

4 回答 4

2

我想出了一个解决方案,灵感来自使用类似std::future. 所以我使用std::future了,并且代码有效。

我所做的只是继承自io_service,并创建一个post_with_future具有返回值未来的新方法。我将不胜感激批评此解决方案以改进它。

#include <iostream>
#include <functional>
#include <type_traits>
#include <future>
#include <boost/asio.hpp>

class future_io_service : public boost::asio::io_service
{
public:
    template <typename FuncType>
    std::future<typename std::result_of<FuncType()>::type> post_with_future(FuncType&& func)
    {
        //keep in mind that std::result_of is std::invoke_result in C++17
        typedef typename std::result_of<FuncType()>::type return_type;
        typedef typename std::packaged_task<return_type()> task_type;
        //since post requires that the functions in it are copy-constructible, we use a shared pointer for the packaged_task since it's only movable and non-copyable
        std::shared_ptr<task_type> task = std::make_shared<task_type>(std::move(func));
        std::future<return_type> returned_future = task->get_future();
        this->post(std::bind(&task_type::operator(),task));
        return returned_future;
    }
};

void SayHello()
{
    std::cout<<"Hello!"<<std::endl;
}

template <typename T>
T GetSum(T a, T b)
{
    std::cout<<"Adding " << a << " and " << b << std::endl;
    return a+b;
}

int main()
{
    future_io_service ioservice;

    ioservice.post(&SayHello);
    auto sum = ioservice.post_with_future(std::bind(&GetSum<int>,1,2));
    ioservice.run();
    std::cout<<sum.get()<<std::endl; //result is 3
    return 0;
}
于 2017-03-21T08:31:32.937 回答
1

这就是您可以通过使用asio::use_futureand来做到这一点的方法async_result。请注意,我通过按值传递事物并使用硬编码参数来求和,从而使示例保持简单。

#include <iostream>
#include <thread>
#include <asio.hpp>
#include <asio/use_future.hpp>

int get_sum(int a, int b)
{
  return a + b;
}

template <typename Func, typename CompletionFunction>
auto perform_asyncly(asio::io_service& ios, Func f, CompletionFunction cfun)
{

  using handler_type = typename asio::handler_type
                         <CompletionFunction, void(asio::error_code, int)>::type;

  handler_type handler{cfun};
  asio::async_result<handler_type> result(handler);

  ios.post([handler, f]() mutable {
        handler(asio::error_code{}, f(2, 2));
      });

  return result.get();
}

int main() {
  asio::io_service ios;
  asio::io_service::work wrk{ios};
  std::thread t{[&]{ ios.run(); }};
  auto res = perform_asyncly(ios, get_sum, asio::use_future);
  std::cout << res.get() << std::endl;

  t.join();

  return 0;
}
于 2017-03-21T19:44:10.517 回答
0

以下解决方案是我计划在我自己的应用程序中使用的解决方案。三大特点:

  1. 函数/lambdas 是 post_function_use_future() 的参数。要求:函数必须返回 void 以外的值,并且它们必须有零输入参数。注意 SayHello() 现在返回一个 int。

  2. 可以使用任何 Asio 上下文,例如 io_context 和 strands。

  3. 在撰写本文时,没有弃用的功能。

在主 cpp 文件中:

#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include "function_return_type.hpp"

template <typename ExecutionContext, typename FuncWithReturnNoArgs>
auto post_function_use_future(ExecutionContext& ctx, FuncWithReturnNoArgs f)
{
    using handler_type = typename boost::asio::handler_type
        <boost::asio::use_future_t<>, void(boost::system::error_code, return_type_t<FuncWithReturnNoArgs>)>::type;

    using Sig = void(boost::system::error_code, return_type_t<FuncWithReturnNoArgs>);
    using Result = typename boost::asio::async_result<boost::asio::use_future_t<>, Sig>;
    using Handler = typename Result::completion_handler_type;

    Handler handler(std::forward<decltype(boost::asio::use_future)>(boost::asio::use_future));
    Result result(handler);

    boost::asio::post(ctx, [handler, f]() mutable {
        handler(boost::system::error_code(), f());
    });

    return result.get();
}

namespace asio = boost::asio;

int SayHello()
{
    std::cout << "Hello!" << std::endl;
    return 0;
}

template <typename T>
T GetSum(T a, T b)
{
    std::cout << "Adding " << a << " and " << b << std::endl;
    return a + b;
}

int main() {
    asio::io_context io;
    auto wg = asio::make_work_guard(io);

    std::thread t{ [&] { io.run(); } };

    auto res1 = post_function_use_future(io, SayHello);
    res1.get(); // block until return value received.

    auto res2 = post_function_use_future(io, []() {return  GetSum(20, 14); });
    std::cout << res2.get() << std::endl; // block until return value received.

    wg.reset();
    if(t.joinable()) t.join();

    return 0;
}

在 function_return_type.hpp 文件中(非常感谢这个解决方案):

#ifndef FUNCTION_RETURN_TYPE_HPP
#define FUNCTION_RETURN_TYPE_HPP

template <typename F>
struct return_type_impl;

template <typename R, typename... Args>
struct return_type_impl<R(Args...)> { using type = R; };

template <typename R, typename... Args>
struct return_type_impl<R(Args..., ...)> { using type = R; };

template <typename R, typename... Args>
struct return_type_impl<R(*)(Args...)> { using type = R; };

template <typename R, typename... Args>
struct return_type_impl<R(*)(Args..., ...)> { using type = R; };

template <typename R, typename... Args>
struct return_type_impl<R(&)(Args...)> { using type = R; };

template <typename R, typename... Args>
struct return_type_impl<R(&)(Args..., ...)> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...)> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...)> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) &> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) &> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) && > { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) && > { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) volatile&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) volatile&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const volatile> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const volatile&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args...) const volatile&&> { using type = R; };

template <typename R, typename C, typename... Args>
struct return_type_impl<R(C::*)(Args..., ...) const volatile&&> { using type = R; };

template <typename T, typename = void>
struct return_type
    : return_type_impl<T> {};

template <typename T>
struct return_type<T, decltype(void(&T::operator()))>
    : return_type_impl<decltype(&T::operator())> {};

template <typename T>
using return_type_t = typename return_type<T>::type;

#endif
于 2018-06-21T17:25:22.443 回答
0

如果目标是有一个简单的单线类绑定函数,它也为您捕获返回值,您可以像这样实现它:

#include <iostream>
#include <boost/asio.hpp>
#include <functional>

using namespace std;

void SayHello()
{
  std::cout<<"Hello!"<<std::endl;
}

template <typename T>
T GetSum(T a, T b)
{
  std::cout<<"Adding " << a << " and " << b << std::endl;
  return a+b;
}

template<typename R, typename F, typename... Args>
auto bind_return_value(R& r, F&& f, Args&&... args)
{
  return [&]()
    {
      r = f(std::forward<Args>(args)...);
    };
}

int main(int argc, char *argv[])
{
  boost::asio::io_service ioservice;

  ioservice.post(&SayHello);
  double sum;
  ioservice.post(bind_return_value(sum, &GetSum<double>, 1, 2));

  ioservice.run();
  std::cout<< sum <<std::endl; //is 3
  return 0;
}
于 2017-03-20T09:46:11.790 回答