线程函数内的c++函数

标签 c++ boost boost-thread

我正在以下列方式创建一个 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/

相关文章:

c++ - Boost 和 Pthread 条件变量的区别

C++1y/14 : Error handling in constexpr functions?

c++ - 没有匹配的成员函数调用 "insert"std::unordered_map

c++ - 如何将无限边缘 boost 为有限边缘?

c++ - 使用 BOOST_FUSION_ADAPT_ADT 调整类时出错

c++ - 指针迭代器的取消引用适配器

c++ - 如何理解父类的层次

c++ - 通用基类合法吗?

C++ boost 线程 : Passing a method of an object

c++ - 如何为单线程 GUI 应用程序创建额外的工作线程?