使用 g++8 和 c++20 的 std::async 编译问题

Compilation problem with std::async using g++8 and c++20

本文关键字:async 编译 问题 std g++8 c++20 使用      更新时间:2023-10-16

我正在尝试使用std::async.我已经写了这段代码

template<transport_type tt>
void stop_update_distances(const std::vector<stop>& stops, stop& from, const general_s& s, const osrm_machine& machine){
//...not important code...
}
void tt_map::update_distances(pqxx::connection& conn, const general_s& s,
const osrm_machine& walk_machine, const osrm_machine& car_machine){
std::vector<std::future<void>> futures;
futures.reserve(stops.size());
for(stop& from : stops){
auto res = std::async(std::launch::async, stop_update_distances<CAR>, stops, from, s, car_machine);
futures.push_back(res);          
}
}

但是 G++8 给我返回了这个错误

src/lib/tt_map.cpp: In member function ‘void kp::mp::tt_map::update_distances(pqxx::connection&, const kp::general_s&, const kp::osrm_machine&, const kp::osrm_machine&)’:
src/lib/tt_map.cpp:383:108: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&)’
auto res = std::async(std::launch::async, stop_update_distances<CAR>, stops, from, s, car_machine);
                                      ^
In file included from src/lib/tt_map.cpp:11:
/usr/include/c++/8/future:1712:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)’
async(launch __policy, _Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/8/future:1712:5: note:   template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = void (&)(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&); _Args = {std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&}]’:
src/lib/tt_map.cpp:383:108:   required from here
/usr/include/c++/8/future:1712:5: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<kp::mp::stop>, kp::mp::stop, kp::general_s, kp::osrm_machine))(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&)>’
/usr/include/c++/8/future:1745:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...)’
async(_Fn&& __fn, _Args&&... __args)
^~~~~
/usr/include/c++/8/future:1745:5: note:   template argument deduction/substitution failed:
/usr/include/c++/8/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_Args>::type ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {void (&)(const std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&), std::vector<kp::mp::stop, std::allocator<kp::mp::stop> >&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&}]’:
src/lib/tt_map.cpp:383:108:   required from here
/usr/include/c++/8/future:1745:5: error: no type named ‘type’ in ‘class std::result_of<std::launch(void (*)(const std::vector<kp::mp::stop>&, kp::mp::stop&, const kp::general_s&, const kp::osrm_machine&), std::vector<kp::mp::stop>, kp::mp::stop, kp::general_s, kp::osrm_machine)>’

我真的不明白。

我在 cppreference.com 中遵循了这个例子。即使它不起作用。

谢谢你的任何提示,有什么问题。

默认情况下,std::thread和类似的类会复制它们的参数,并按值将它们传递给线程函数。这可能不是您想要的。编译器错误的具体原因是stop_update_distances通过非常量引用获取stop& from参数,并且无法将临时副本传递到那里。

不知道stop_update_distances如何使用其参数就很难判断,但我的猜测是,您可能希望通过引用传递它们。您可以根据需要将它们包装在std::refstd::cref中。像这样:

auto res = std::async(
std::launch::async,
stop_update_distances<CAR>,
std::cref(stops),
std::ref(from),
std::cref(s),
std::cref(car_machine));

小心发出的生存期和访问同步 - 您现在在线程之间共享对象。