c ++ Thread函数内的函数

时间:2015-11-11 05:58:10

标签: c++ boost boost-thread

我正在以下列方式创建一个提升线程。

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的对象传递给它?

1 个答案:

答案 0 :(得分:0)

是的,你可以从另一个函数调用一个函数。函数调用将在与原始函数相同的线程中执行。要将指向该类的指针传递给另一个函数,请执行与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;
}
相关问题