简体   繁体   English

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

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

I am trying to use std::async . 我正在尝试使用std::async I have written this code 我已经写了这段代码

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);          
  }

}

but g++8 returned me this error 但是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)>’

which I don't really understand. 我不太了解。

I have followed the example in cppreference.com . 我遵循cppreference.com中的示例。 Even though it doesn't work. 即使它不起作用。

Thank you for any hint, what is wrong. 谢谢您的提示,这是怎么回事。

std::thread and similar classes by default make copies of their arguments, and pass them along to the thread function by value. 默认情况下, std::thread和类似类会复制其参数,然后按值将其传递给线程函数。 This is probably not what you want here. 这可能不是您想要的。 The specific cause of the compiler error is that stop_update_distances takes stop& from parameter by non-const reference, and a temporary copy cannot be passed there. 导致编译器错误的具体原因是stop_update_distances通过非const引用stop& from参数中获取stop& from ,并且无法将临时副本传递到那里。

It's hard to tell without knowing how stop_update_distances is expected to use its parameters, but my guess is, you probably want to pass them along by reference. 在不知道stop_update_distances如何使用其参数的情况下很难说,但是我的猜测是,您可能希望通过引用传递它们。 You do this by wrapping them in std::ref or std::cref as needed. 为此,您可以根据需要将它们包装在std::refstd::cref中。 Something like this: 像这样:

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

Be careful with lifetime and access synchronization issued - you are now sharing objects between threads. 注意生命周期和发出的访问同步-现在正在线程之间共享对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM