使用派生的 class 的成员函数启动线程
Start thread with derived class's member function
类似于这个问题:Start thread with member function and this one: std::thread calling method of class
但是我有以下几点:
#include <thread>
#include <iostream>
class myAbstractClass {
public:
virtual void myFunction() = 0;//abstract class
}
class myFirstClass : public myAbstractClass {
public:
void myFunction() { std::cout << "First class here";}
}
class mySecondClass : public myAbstractClass {
public:
void myFunction() { std::cout << "Second class here";}
}
然后我必须在新线程中从不同的地方调用 myFunction()
,但以下代码无法编译(我想不出其他任何尝试):
public void callMemberFunctionInThread(myAbstractClass& myInstance) {
std::thread myThread (&myAbstractClass::myFunction, myInstance);
//supposed to call myInstance.myFunction() on myThread
}
类似于这个问题:Start thread with member function and this one: std::thread calling method of class
但是我有以下几点:
#include <thread>
#include <iostream>
class myAbstractClass {
public:
virtual void myFunction() = 0;//abstract class
}
class myFirstClass : public myAbstractClass {
public:
void myFunction() { std::cout << "First class here";}
}
class mySecondClass : public myAbstractClass {
public:
void myFunction() { std::cout << "Second class here";}
}
然后我必须在新线程中从不同的地方调用 myFunction()
,但以下代码无法编译(我想不出其他任何尝试):
public void callMemberFunctionInThread(myAbstractClass& myInstance) {
std::thread myThread (&myAbstractClass::myFunction, myInstance);
//supposed to call myInstance.myFunction() on myThread
}