【问题标题】:boost coroutine using with boost future使用 boost future 来提升协程
【发布时间】:2018-09-29 10:57:12
【问题描述】:

我正在尝试制作具有协程结果的地图。
我搜索了一个展示如何获得协程未来的示例。
之后我需要生成任务以将它们与 asio 一起使用。
这是协程的代码:

std::map<std::string, boost::shared_ptr<HTTPResponse>> create_tasks(const symbols_enum& symbol, date day)
{
    int start = 0;

    if (is_dst(day))
    {
        start = 1;
    }

    ostringstream oss;
    oss << symbol;
    std::string url_currency{oss.str()};
    std::ostringstream().swap(oss); // swap m with a default constructed stringstream
    std::string url_year{day.year()};

    stringstream ss_month;
    ss_month << setw(2) << setfill('0') << ((day.month().as_number()) - 1);
    string url_month{ ss_month.str() };
    std::stringstream().swap(ss_month); // swap m with a default constructed stringstream

    stringstream ss_day;
    ss_day << setw(2) << setfill('0') << day.day().as_number();
    string url_day{ ss_day.str() };
    std::stringstream().swap(ss_day); // swap m with a default constructed stringstream

    std::string URL{ protocol_host_URL+"/"+"datafeed"+"/"+ url_currency +"/"+ url_year +"/"+ url_month +"/"+ url_day +"/" };

    HTTPClient client;


    std::map<std::string, std::shared_ptr<HTTPRequest>> requests_variables;
    std::map<std::string, boost::shared_ptr<HTTPResponse>> tasks;

    for (int hour = 0;hour < 24;hour++)
    {
        stringstream ss_hour;
        ss_hour << setw(2) << setfill('0') << hour;
        string url_hour{ ss_hour.str() };
        std::stringstream().swap(ss_hour); // swap m with a default constructed stringstream
        URL = URL + url_hour +"h_ticks.bi5";

        std::string request_name = "request_" + std::to_string(hour);
        requests_variables[request_name] =  client.create_request(hour, URL);

        //requests_variables[request_name]->execute();//must be coroutine and i think it should be normal coroutine
        coroutine<boost::shared_ptr<HTTPResponse>>::pull_type response_future_coroutine_pull(requests_variables[request_name]->execute);
        tasks[request_name] = response_future_coroutine_pull.get();         

    }
    //tasks = [asyncio.ensure_future(get]


        return tasks;

    }

这部分代码显示了我需要执行未来任务的方式。
它基于 python 获取协程期货的方式。
我无法理解如何在 C++ 中执行此操作。
我部分使用了 asio,但为了使用 for 循环,我想制作将包含在 for 循环中的协程,其异步函数将在 asio 服务中运行。
我不知道我的想法是否正确。 我特别需要的是一个如何制作协程期货容器的示例......

【问题讨论】:

  • 您不会“获得 coro 的未来”。当然,你可以从里面承诺一个。

标签: c++ multithreading boost boost-asio boost-coroutine


【解决方案1】:

这里不需要boost::coroutine。你要的是std::aysnc

std::map<std::string, std::future<boost::shared_ptr<HTTPResponse>>> tasks;
for (...) {
    ...
    tasks[request_name] = std::async(&HTTPRequest::execute, client.create_request(hour, URL));
}

当你想等待回复时

std::map<std::string, boost::shared_ptr<HTTPResponse>> responses;
for (auto& pair : tasks) {
    response[pair.first] = pair.second.get();
}

【讨论】:

  • 感谢您的回复,但异步将启动新线程...我只想使用最少的线程来实现它...这是出于教育目的...我需要知道如何与 co 合作具有最少线程的例程......以及是否有共同例程的未来
  • 启动策略std::launch::deferred 不会启动任何线程。默认启动策略允许在每次调用的基础上选择线程执行和延迟执行。 See here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多