c++ - 在 C++ 中使用线程调用具有多个参数的非静态函数

标签 c++ multithreading c++11 pthreads

<分区>

'pthread_create (thread, attr, start_routine, arg)'

我可以调用一个非静态函数来创建具有多个参数的线程吗,因为 pthread_create() 将只接受一个参数并且是 void 类型。

我在我的类中使用线程,它有许多执行繁重任务的函数,所以我想在它们自己的线程上执行每个函数,但我不能这样做,因为 Pthrad_create 只接受一个参数和类型的函数静态的,那么我该如何解决这个问题。

谢谢。

最佳答案

要完成有关 pthread 的其他答案:

因为你标记了 C++11,所以完全避免 pthread,并使用 std::thread :

std::thread t(func1,a,b,c,d);

它是可移植的,更易于维护。

编辑:

如您所问,您可以找到std::thread 文档here .

SO post discussed也传递多个参数:

#include <thread>

void func1(int a, int b, ObjA c, ObjB d){
    //...
}

int main(int argc, char* argv[])
{
    std::thread t(func1,a,b,c,d);
}

关于c++ - 在 C++ 中使用线程调用具有多个参数的非静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029487/

相关文章:

c++ - 最容易使用连接器/接口(interface)来使用 C++ 的 MySQL/其他数据库

c++ - “Cannot find -l<directory>”错误-CMake

c++ - 从重定向的标准输入获取输入时使用 seekg()

java - Cipher 线程安全吗?

c++ - C++11 委托(delegate)的 ctors 是否比调用 init 函数的 C++03 ctors 表现更差?

c++ - Qt:RemoveWidget和对象删除

java - 线程转储分析(AWT-EventQueue 可运行但等待条件)

c++ - 多种类型的模板方法特化

c++ - 为什么我不能将一个枚举作为另一个枚举的基础类型?

c++ - Boost (v1.33.1) 线程中断