本文介绍了线程和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在玩C ++和pthreads,到目前为止这么好。我可以访问类成员函数,如果它是静态的,我已经阅读,我可以访问一个普通的类成员函数,如果我传递this作为参数与pthread_create,因为c ++这样做。但我的问题是,我想给一个int函数,我不知道如何使用pthread_create做多个参数。
I am playing with C++ and pthreads and so far so good. I can access a class member function if it's static and I've read that I can access a normal class member functions if I pass "this" as an argument with pthread_create, because c++ does this to, under the hood. But my problem is that I want to give an int to that function, and I don't know how to do multiple arguments with pthread_create.
推荐答案
传递结构指针。
struct Arg {
MyClass* _this;
int another_arg;
};
...
Arg* arg = new Arg;
arg->_this = this;
arg->another_arg = 12;
pthread_create(..., arg);
...
delete arg;
这篇关于线程和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!