【发布时间】:2021-12-29 05:14:57
【问题描述】:
我正在尝试运行一个异步函数,向它传递一个要执行的函数 f 和一个模板函数 f0 作为属性。
这是我通过模板创建的函数
DivideVerticesInThreads<0> f0(g, {}, {});
模板是
template <int NSegments, int Segment> struct SegmentVertices {
std::hash<Graph::vertex_descriptor> _h;
bool operator()(Graph::vertex_descriptor vd) const { return (_h(vd) % NSegments) == Segment; }
};
template <int N>
using DivideVerticesInThreads = boost::filtered_graph<Graph, boost::keep_all, SegmentVertices<4, N>>;
那我就这样调用async函数
auto handle = async(std::launch::async,f, f0);
然后我将函数 f 传递给它:
auto f = [&](G const& f0) {
for(vp = vertices(f0); vp.first != vp.second; ++vp.first){
//...
}
};
完整的代码是:
template<typename G>
void parallel_count_adj_luby(G g){
property_map<Graph,vertex_degree_t>::type deg = get(vertex_degree, g);
auto f = [&](G const& g1) {
for(vp = vertices(g1); vp.first != vp.second; ++vp.first){
// ...
}
};
DivideVerticesInThreads<0> f0(g, {}, {});
auto handle = async(std::launch::async,f, f0);
}
问题是异步函数给了我这个错误
error: no matching function for call to 'async(std::launch, parallel_count_adj_luby(G) [with G = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_color_t, int, boost::property<boost::vertex_degree_t, int> > >]::<lambda(const boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::property<boost::vertex_color_t, int, boost::property<boost::vertex_degree_t, int> > >&)>&, DivideVerticesInThreads<0>&)'
auto handle = async(std::launch::async,f, f0);
我不太了解 c++ 和未来,所以错误可能是虚拟的。我做错了什么?
【问题讨论】:
-
G中的auto f = [&](G const& f0)是什么? -
G和DivideVerticesInThreads<0>一样吗? (请阅读minimal reproducible example。) -
G 只是一个模板
请放弃半解释并发布minimal reproducible example。另外,发布完整的编译器错误。 async是什么? -
我希望我添加所有,尊重minimal reproducible example。
-
根据错误
DivideVerticesInThreads<0>不是G也不能隐式转换为它。
标签: c++ asynchronous boost future