<分区>
'pthread_create (thread, attr, start_routine, arg)'
我可以调用一个非静态函数来创建具有多个参数的线程吗,因为 pthread_create() 将只接受一个参数并且是 void 类型。
我在我的类中使用线程,它有许多执行繁重任务的函数,所以我想在它们自己的线程上执行每个函数,但我不能这样做,因为 Pthrad_create 只接受一个参数和类型的函数静态的,那么我该如何解决这个问题。
谢谢。
<分区>
'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/