我正在以下列方式创建一个 boost 线程。
static void* ThreadFuncCall(void *arg) { return ((TestClass*)arg)->Thread1Func((TestClass*)arg
Thread1 = new boost::thread((boost::bind(&TestClass::ThreadFuncCall, this)));
void* TestClass::Thread1Func(TestClass* testClass){
//Accessing the member variables here
testClass->m_active = true;
//call another function here
}
在 Thread1Func
中,我想调用另一个函数,我想将 TestClass
的对象传递给它,就像我传递给 ThreadFuncCall
.
我想知道我们是否可以在线程函数中调用另一个函数?
如果可以,那么如何将 TestClass
的对象传递给它?
最佳答案
是的,您可以从另一个函数调用一个函数。函数调用将在与原始函数相同的线程中执行。要将指向类的指针传递给另一个函数,请执行与 ThreadFunc1 相同的操作。
void* TestClass::Thread1Func(TestClass* testClass){
//Accessing the member variables here
testClass->m_active = true;
//call another function here
int i = function1(testClass, "hello world");
}
int function1(TestClass* testClass, char* text){
testClass->printText(text);
return 7;
}
关于线程函数内的c++函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645025/