c++多线程类方法
c++ multithreading class methods
我有以下问题:
vector<thread> vThreads;
list<Crob *> lRobs;
list<Crob *>::iterator i;
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(thread((*i)->findPath));
}
我想传递方法findPath到一个线程,但我只是得到很多错误…
> labrob.cpp: In function ‘int main(int, char**)’:
labrob.cpp:72:43: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
labrob.cpp:72:43: note: candidates are:
In file included from labrob.cpp:14:0:
/usr/include/c++/4.7/thread:131:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (Crob::*)(); _Args = {}]
/usr/include/c++/4.7/thread:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (Crob::*&&)()’
/usr/include/c++/4.7/thread:126:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.7/thread:126:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.7/thread:122:5: note: std::thread::thread()
/usr/include/c++/4.7/thread:122:5: note: candidate expects 0 arguments, 1 provided
make: *** [labrob.o] Error 1
我已经尝试过传递本地函数,并且没有问题…
新增CRob header
#pragma once
#include "point.hpp"
#include "lab.hpp"
class Crob
{
protected:
Cpoint *pos;
int steps;
Clab *labfind;
string direction;
public:
Crob(Clab *lab);
virtual ~Crob();
virtual void findPath();
void moveTo(int x, int y);
void moveToPrint(int x, int y);
int getSteps(void);
void checkDirection();
};
看起来您试图将非静态方法传递给std::thread构造函数。您不能这样做:非静态方法需要一个对象才能被调用。看起来你想要:
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(std::thread(&Crob::findPath, *i));
}
相关文章:
- 使用基类中的派生方法运行线程,而无需使用模板
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++多线程程序:变量定义为类成员的隔离错误
- C++中的多线程:连接线程的正确方法
- 工作器/控制器多线程和接口类
- C++ 提升类内的多线程
- 为多线程环境包装 c++ new/delete 的安全/好方法
- C 多线程JAVA JNI方法调用
- MFC多线程程序可以以并行方法运行
- 使用C 的多线程应用程序中测量全局时间(壁挂)的最快方法
- 如何在类中进行 c++ 多线程处理(将线程引用保留为成员 var)
- 类 C++ 中的多线程非静态
- 如何使用pthreads使多线程正常工作,而不是使用C++在类中提升
- 引用计数类和多线程
- ISO禁止启动自己类方法的线程
- 类的多线程和全局实例
- 使用蒙特卡罗方法多线程计算pi值
- c++多线程类方法
- c++:如何定义类方法作为线程的启动例程(使用pthread库)
- TCPStream类用于多线程TCP服务器