class Map {
public:
Bird *pB;
Pig *pP;
..........
}
class Grav {
public:
Map *pM;
......
void runBird(Bird &b);
void runPig(Pig &p);
void run(Map &m);
}
void Grav::run(Map &m) {
thread t[2];
t[0] = thread(&Grav::runBird, std::ref(m.pBird[0]));
t[1] = thread(&Grav::runPig, std::ref(m.pPig[0]));
t[0].join();
t[1].join();
}
Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' AngryBird h:\visual studio\vc\include\type_traits 1501
线程入口函数可以是类成员函数吗? 貌似不行,只能是全局函数或者类的静态成员函数。 但是,可以在开辟线程时将this指针传递给线程入口函数,然后再调用类成员函数。 // 子线程入口DWORD WINAPI ThreadProc( LPVOID lpParameter ){ // 调用CCamProjDlg类成员函数 ((CCamProjDlg*)lpParameter......
答案就在这里:线程入口函数可以是类成员函数吗?
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
http://blog.sina.com.cn/s/blog_478041a70100g2b5.html
把runBird(),runPig()改为static为什么在你的程序中不可行? 是因为静态成员函数无法调用成员变量? 那你可以将runBird和runPig添加参数接收
类的指针,就可以访问成员变量 或者 创建线程时将类指针传递给线程, 然后在线程函数中获取类指针
类静态成员函数:
static void * start_thread(void *arg);
创建线程
pthread_create(&pid,NULL,start_thread,(void *)this) //这里的this会作为start_thread的传参
这样就把this指针传给了静态成员函数