我试图理解C ++中的线程,但不知道如何解决此问题。
我想调用两个线程来运行名为“ createS”的函数,但出现此错误:
错误:无效使用非静态成员函数
我已经阅读了有关此主题的其他问题,但是我真的不明白如何使我的代码正常工作。
有人可以向我解释我做错了什么,并尝试帮助我找到解决方案吗?
test_class.cpp
void test_class::generateS(){
map1=new multimap<double,vector<int>>;
map2=new multimap<double,vector<int>>;
thread thread_1( createS, 0, nCells/2, map1 );
thread thread_2( createS, nCells/2, nCells, map2);
thread_1.join();
thread_2.join();
}
void test_class::createS(int startP, int endP, Costs *mapPointer){
//i do some stuff
}
test_class.h
void createS(int start, int end, Costs *mapPointer);
void generateS();
thread thread_1(&test_class::createS, this, 0, nCells/2, map1);
thread thread_2(&test_class::createS, this, nCells/2, nCells, map2);
注意:如果createS
不依赖于对象状态,则最好使其成为static
类成员,并按照您的方式进行调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句