0

I have a loop that pushes back calls of std::async that are used to create objects in the pointed function and emplace them back to another vector. All the calls are pushed to the futures function and the results are ready when i used the VS debugger. However of the 507 calls, only 30 objects are actually created and i cant seem to pin point why.I have tried setting the launch policy to both async and defered but get the same result.

void load_sec_p(vector<Security>* secs, map<string, map<string, vector<daySec>>> *psa_timeline,security sec) {
    Security tmp = Security(psa_timeline, &sec.tsymb, &sec.gicsInd);
    std::lock_guard<std::mutex> lock(s_SecsMutex);
    secs->emplace_back(tmp);
}

Above is the function being executed in the async call below is the loop that pushes back the futures

for (auto& sec : security_list) {
            m_SecFutures.emplace_back(std::async(load_sec_p,&async_secs, &psa_timeline, sec));
        }

The following pictures show the watch of both variables after the above loop is completed and the entire future vectors is checked for completion. async_vector watch

futures vector watch

I have tried creating the objects by just using a regular for loop and appending them synchronously but it simply just takes too long(2 hours and 11 minutes long). If anyone has any advice on alternatives or how to fix my vector problem it would be greatly appreciated.

The code that checks if all the futures is shown below:

        bool done = false;
        cout << "Waiting...";
        do {
            done = futures_ready(m_SecFutures);
        } while (!done);     

The function is

template<class T>
bool futures_ready(std::vector<std::future<T>>& futures) {
std::chrono::milliseconds span(5);

bool finished = false;
int pends = 0;

while (!finished) {
    //allowing thread to sleep so futures can process a bit more and also
    //so thread doesnt reach max cpu usage
    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    for (auto& x : futures) {
        if (x.wait_for(span) == std::future_status::timeout) {
            ++pends;
        }
    }
    if (pends == 0) {
        finished = true;
    }
    else {
        pends = 0;
    }
}

return finished;
}
Chill Kid
  • 11
  • 4
  • Are you calling `get()` on those futures? What exactly does "future vectors is checked for completion" mean? – Igor Tandetnik May 17 '20 at 13:49
  • I use a function to loop through the vector comparing each of the elements status to std::future_status::timeout, and if all are not , then it assumes all elements are completed. – Chill Kid May 17 '20 at 20:00
  • How do you determine each element's status? `std::future` doesn't directly expose it. Show the code where you are doing that. Anyway, `std::future_status` has three possible values: `ready`, `timeout` and `deferred`. If the status is not `timeout` but the element is not ready, then most likely the status is `deferred`. You probably want to pass `std::launch::async` as the first parameter to your `async` calls. – Igor Tandetnik May 17 '20 at 20:31
  • Also, i did use async as the earlier launch policy but still only 34 objects were created. – Chill Kid May 17 '20 at 20:42
  • Well, what does `futures_ready` do? In any case, the right way to wait for a future to be ready is to call `get()` on it. – Igor Tandetnik May 17 '20 at 20:45
  • The function will be included above, sorry for the exclusion – Chill Kid May 17 '20 at 20:56
  • You are checking for `std::future_status::timeout` but not for `std::future_status::deferred`. Again, `std::future_status` has three possible values, not two. – Igor Tandetnik May 17 '20 at 21:02
  • Ohhhh, okay, i thought deffered was only possible if std::launch::async was not passed – Chill Kid May 17 '20 at 21:16
  • It also checked for deferred status and it is still only creating 32 objects – Chill Kid May 17 '20 at 22:43

0 Answers0