0

我有一个相当复杂的代码,其中包含std::async调用和std::packaged_task,但无法执行到最后。我将其简化为最小的可重现示例。

两个 async 函数一个接一个地被调用,其中有 packaged_tasks 使用std::async. future.wait()然后我们都使用相应的方法等待两个异步函数完成。执行停止在futY.wait();并且第二个packaged_task永远不会执行(没有第二个Inside handler func日志)。

#include <iostream>     // std::cout
#include <future>       // std::packaged_task, std::future
#include <exception>
#include <vector>
#include <list>
#include <memory>
#include <functional>

std::list<std::function<bool(const std::vector<int> &data)>> handlers_;

std::future<std::vector<int>> countup(int from, int to) {
    std::function<std::vector<int>(std::exception_ptr, std::vector<int>)> func = [=] (std::exception_ptr ex, std::vector<int> data) {
        std::cout << "Inside handler func " << from << " " << to << std::endl;
        if (ex != nullptr) {
            std::rethrow_exception(ex);
        }
        return data;
    };
    auto packageP = std::make_shared<std::packaged_task<std::vector<int>(std::exception_ptr, std::vector<int>)>>(func);

    auto fut = packageP->get_future();
    handlers_.push_back([packageP] (const std::vector<int> &data) mutable -> bool {
            std::cout << "Calling handler with data, size: " << data.size() << std::endl;

            (*packageP)(nullptr, data);
            return data.size();
        }
    );

    auto fut2 = std::async(std::launch::async, [=, &handlers_] {
            std::cout << "Before handler called " << from << to << std::endl;

            std::vector<int> vec ( to, from );

            auto res = (*handlers_.begin())(vec);

            std::cout << "Handler result " << res << " for " << from << " " << to << std::endl;
        });

    std::cout << "Called async in countup for " << from << " " << to << std::endl;

    return fut;
}

int main ()
{
  auto futX = std::async(std::launch::async, [] {
      auto fut1 = std::async(std::launch::async, [] {
        auto fut2 = countup(0, 2);

        std::cout << "Called X countup and waiting to finish" << std::endl;

        fut2.wait();

        auto vec = fut2.get();
        std::cout << "The X countup returned" << std::endl;
      });
      std::cout << "Called X async internal and waiting to finish" << std::endl;
      fut1.wait();

      return 2;
  });

  std::cout << "Called async X and waiting to finish" << std::endl;

  auto futY = std::async(std::launch::async, [] {
      auto fut1 = std::async(std::launch::async, [] {
        auto fut2 = countup(0, 3);

        std::cout << "Called Y countup and waiting to finish" << std::endl;
        fut2.wait();

        auto vec = fut2.get();
        std::cout << "The Y countup returned " << std::endl;
      });
      std::cout << "Called Y async internal and waiting to finish" << std::endl;
      fut1.wait();

      return 3;
  });

  std::cout << "Called async Y and waiting to finish" << std::endl;

  futX.wait();
  std::cout << "After async X and waiting to finish" << std::endl;

  futY.wait();
  std::cout << "After async Y and waiting to finish" << std::endl;

  int valueX = futX.get();                  // wait for the task to finish and get result
  int valueY = futY.get();                  // wait for the task to finish and get result

  std::cout << "The countdown lasted for " << valueX  << " " << valueY << " seconds" << std::endl;

  return 0;
}

日志如下:

Called async X and waiting to finish
Called async Y and waiting to finish
Called X async internal and waiting to finish
Called Y async internal and waiting to finish
Called async in countup for Before handler called 02
0 2
Calling handler with data, size: 2
Inside handler func 0Called async in countup for  2
Handler result 01 for 0 2
 Before handler called 03
3Calling handler with data, size: 
Called X countup and waiting to finish
The X countup returned
3
After async X and waiting to finish
Called Y countup and waiting to finish

您能否解释一下为什么代码直到最后才执行?

4

1 回答 1

0

当您调用时,您正在重复使用相同的打包任务(*handlers_.begin()),并且我认为您在调用时遇到了数据竞争handlers_.push_back

你不需要全局handlers_,你可以捕获一个本地处理程序。

#include <iostream>     // std::cout
#include <future>       // std::packaged_task, std::future
#include <exception>
#include <vector>
#include <list>
#include <memory>
#include <functional>

std::future<std::vector<int>> countup(int from, int to) {
    auto func = [=] (std::exception_ptr ex, std::vector<int> data) {
        std::cout << "Inside handler func " << from << " " << to << std::endl;
        if (ex != nullptr) {
            std::rethrow_exception(ex);
        }
        return data;
    };
    auto packageP = std::make_shared<std::packaged_task<std::vector<int>(std::exception_ptr, std::vector<int>)>>(func);

    auto fut = packageP->get_future();
    auto handler = [packageP] (const std::vector<int> &data) mutable -> bool {
        std::cout << "Calling handler with data, size: " << data.size() << std::endl;

        (*packageP)(nullptr, data);
        return data.size();
    };

    auto fut2 = std::async(std::launch::async, [=]() mutable {
            std::cout << "Before handler called " << from << to << std::endl;

            std::vector<int> vec ( to, from );

            auto res = handler(vec);

            std::cout << "Handler result " << res << " for " << from << " " << to << std::endl;
        });

    std::cout << "Called async in countup for " << from << " " << to << std::endl;

    return fut;
}

int main ()
{
  auto futX = std::async(std::launch::async, [] {
      auto fut1 = std::async(std::launch::async, [] {
        auto fut2 = countup(0, 2);

        std::cout << "Called X countup and waiting to finish" << std::endl;

        fut2.wait();

        auto vec = fut2.get();
        std::cout << "The X countup returned" << std::endl;
      });
      std::cout << "Called X async internal and waiting to finish" << std::endl;
      fut1.wait();

      return 2;
  });

  std::cout << "Called async X and waiting to finish" << std::endl;

  auto futY = std::async(std::launch::async, [] {
      auto fut1 = std::async(std::launch::async, [] {
        auto fut2 = countup(0, 3);

        std::cout << "Called Y countup and waiting to finish" << std::endl;
        fut2.wait();

        auto vec = fut2.get();
        std::cout << "The Y countup returned " << std::endl;
      });
      std::cout << "Called Y async internal and waiting to finish" << std::endl;
      fut1.wait();

      return 3;
  });

  std::cout << "Called async Y and waiting to finish" << std::endl;

  futX.wait();
  std::cout << "After async X and waiting to finish" << std::endl;

  futY.wait();
  std::cout << "After async Y and waiting to finish" << std::endl;

  int valueX = futX.get();                  // wait for the task to finish and get result
  int valueY = futY.get();                  // wait for the task to finish and get result

  std::cout << "The countdown lasted for " << valueX  << " " << valueY << " seconds" << std::endl;

  return 0;
}

在大肠杆菌上看到它

于 2021-08-17T08:59:46.637 回答