I want to call a method with async multiple times. A simplified example is shown below:
size_t counter(std::string &s)
{
return s.size();
}
void stringCountAccumulator()
{
std::vector<std::string> foos = {"this", "is", "spartaa"};
size_t total = 0;
for (std::string &s : foos)
{
std::future<size_t> fut = std::async(
std::launch::async,
counter, s);
total += fut.get();
}
std::cout << "Total: " << total;
}
It seems that, fut.get() blocks other future calls. How can I implement this problem in C++? I need to call a function in a separate thread. This function "returns" a value.