H
H
HamsterGamer2021-01-01 20:08:18
C++ / C#
HamsterGamer, 2021-01-01 20:08:18

Why is my asynchronous HTTP client taking a long time to execute?

Hello everyone, you need to implement an asynchronous HTTP client, which, having a list of tracker urls, should return the first received HTTP response from any first responder. Having slightly rewritten the example from the docks - https://www.boost.org/doc/libs/1_76_0/doc/html/boo... and adding work with boost::future/boost::promise there, I wrote the following code:
https://pastebin.com/hAG2k3rd
Changes made to the example: the client structure now has a promise, the client code has a future from it, and using the when_any boost function, it tries to get the fastest and most valid future. When_any will return a vector of futures, where the first one will be guaranteed to be ready for processing, inside the handler I check the result of the future for an exception, and if there is one, then I start the procedure again, but with a part of the futures that have not yet been processed.

In general, it seems to even work, but for some reason it slows down (namely, the entire program, that is, the main thread and the thread where io_service:: run is running freeze) on line number 228 "result.set_value(any_res->get() );", and I do not understand what is the reason for this behavior, please help me figure it out! Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HamsterGamer, 2021-01-02
@HamsterGamer

It seems like I figured it out, in the literature of Anthony Williams, when_any (from experimental std) works exactly as I described above, that is, when at least 1 future is ready, it will return the vector of futures in a sorted form so that the ready ones go first (that is, begin should be 100% is_ready), in the boost version, the finished future will be in its place, and not at the beginning.
And you need to get it somehow like this:

...(boost::future<std::vector<boost::future<std::string>>> result_args) {
                auto results = result_args.get();
                auto any_res = results.begin();
                for (; any_res != results.end(); any_res++){
                    if (any_res->is_ready())
                        break;
                }
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question