我想要使用一個類的函數的std ::線程C++的std ::螺紋和方法類
後續的代碼片段返回一個錯誤
MyClass *MyClass_ptr = new MyClass;
MyClass_ptr->MyFunction(); // Works
std::thread ThreadA(MyClass_ptr->MyFunction()); // Error here
std::thread ThreadB(MyClass_ptr->MyOtherFunction()); // Error here
我需要做一個線程與指向該類的特定指針:MyClass_ptr
那麼,有沒有辦法使用該特定指針的那個類的方法?
如果它是有用的這裏是與Microsoft Visual Studio 2013
#include "stdafx.h"
#include <iostream>
#include <thread>
class MyClass
{
public:
void MyFunction();
void MyOtherFunction();
};
void MyClass::MyOtherFunction()
{
std::cout << "Inside MyOtherFunction" << std::endl;
std::cin.get();
}
void MyClass::MyFunction()
{
std::cout << "Inside MyFunction" << std::endl;
std::cin.get();
}
int _tmain(int argc, _TCHAR* argv[])
{
MyClass *MyClass_ptr = new MyClass;
MyClass_ptr->MyFunction(); // Works
std::thread ThreadA(MyClass_ptr->MyFunction()); // Error here
std::thread ThreadB(MyClass_ptr->MyOtherFunction()); // Error here
delete MyClass_ptr;
MyClass_ptr = nullptr;
return 0;
}
您不需要使用綁定。 – 2014-09-02 22:20:22
Thx很大程度上代碼 – Michele 2014-09-02 22:21:42
確定uop決定傳入參數 – Rajeshwar 2014-09-02 22:22:13