如何使用C ++ 11线程类在单独的线程中执行类成员函数? [英] How to execute a class member function in a separate thread using C++11 thread class?
问题描述
我正在尝试使用C ++ 11的 std :: thread
类来运行要并行执行的类的成员函数。
I'm trying to use the C++11's std::thread
class to run a member function of a class to execute in parallel.
头文件的代码类似于:
class SomeClass {
vector<int> classVector;
void threadFunction(bool arg1, bool arg2);
public:
void otherFunction();
};
cpp文件类似于:
void SomeClass::threadFunction(bool arg1, bool arg2) {
//thread task
}
void SomeClass::otherFunction() {
thread t1(&SomeClass::threadFunction, arg1, arg2, *this);
t1.join();
}
我正在Mac OS X 10.8.3下使用Xcode 4.6.1。我使用的编译器是Xcode随附的Apple LLVM 4.2。
I am using Xcode 4.6.1 under Mac OS X 10.8.3. The compiler I am using is Apple LLVM 4.2 which came with the Xcode.
上面的代码不起作用。编译器错误表明试图使用已删除的函数
。
The above code does not work. The compiler error says that "Attempted to use deleted function"
.
在线程创建的行中,它显示了
On the line of thread creation it shows the following massage.
In instantiation of function template specialization 'std::__1::thread::thread<void (SomeClass::*)(bool, bool), bool &, bool &, FETD2DSolver &, void>' requested here
我是C ++ 11和线程类的新手。有人可以帮我吗?
I'm new in C++11 and the thread class. Could someone help me?
推荐答案
实例应该是第二个参数,例如:
The instance should be the second argument, like so:
std::thread t1(&SomeClass::threadFunction, *this, arg1, arg2);
这篇关于如何使用C ++ 11线程类在单独的线程中执行类成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!