Search code examples
c++multithreadingboostboost-asioboost-coroutine

boost coroutine using with boost future


I am trying to make map having the result of coroutine.
I searched for an example showing how to get future of coroutine.
I need to generate tasks to use them with asio afterwards.
this is the code for coroutine:

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;

    }

This part of the code is showing the way I need to make future tasks.
It is based on the way python do to get futures for coroutines.
I could not understand how to do this in c++.
I am using asio in part but for the sake of using for loop I thought of making coroutine which will be contained in the for loop and its async functions will be run in asio service.
I don't know if my thinking is even right. what I specifically need is an example of how to make a container of futures of coroutines...


Solution

  • You don't need boost::coroutine here. What you want is 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));
    }
    

    When you want to wait for the responses

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