pthread_create参数函数指针 [英] pthread_create parameter function pointer
问题描述
我试图在下面的代码中创建一个线程,但指向pthread_create方法调用的函数参数的指针只是不让我编译我的代码。
请让我知道我在做什么错误,以及如何在以下代码中修复它:
#includeRobotNodes.cpp
int main(int argc,char ** argv){
int i,numRobotsToInit = 7 ;
//声明运行函数指针
void(* run)();
//创建机器人节点数组
RobotNodes * robots [numRobotsToInit];
// init robot nodes
for(i = 0; i robots [i] = new RobotNodes(i,0.2,0.2)
}
for(i = 0; i run =& robots [i]
pthread_t thread;
pthread_create(& thread,NULL,(void *(*)(void *))run,NULL);
}
}
我得到的错误如下:
错误:需要作为一元&'操作数的$值$ / code>
编辑:run类RobotNodes.cpp包含在此类的顶部。
似乎有一个非静态 RobotNodes
中的成员函数,你似乎认为成员函数的类型是 void(*)()
。如果是这样,那么你错了。
成员函数和自由函数的类型不相同,即使它们具有完全相同的签名!
所以我建议你定义一个名为 start
的静态函数:
class RobotNodes
{
public:
void run(); //定义它像你喜欢
static void * start(void * pdata)
{
RobotNodes * robot = static_cast< RobotNodes *>(pdata);
robot-> run(); // forward it
return 0; //或返回为文档说
}
};
然后使用开始
: >
std :: vector< pthread_t>线程(numRobotsToInit);
for(i = 0; i {
pthread_create(& threads [i],NULL,RobotNodes :: start,& robots [i]);
}
另外,注意我创建了一个 pthread_t
在循环外;这是因为如果它们是不同的线程,每个线程实例必须不同,此外,即使循环停止后,每个线程实例必须继续存在。
I am trying to create a thread in the following code but the pointer to a function parameter of the pthread_create method call is just not letting me compile my code.
Please let me know what I am doing wrong and how can I fix it in the following code:
#include "RobotNodes.cpp"
int main(int argc, char** argv){
int i, numRobotsToInit = 7;
//declare run function pointer
void (*run)();
//create array of robot nodes
RobotNodes* robots[numRobotsToInit];
//init robot nodes
for(i = 0; i<numRobotsToInit; i++){
robots[i] = new RobotNodes(i, 0.2, 0.2);
}
for(i = 0; i<numRobotsToInit; i++){
run = &robots[i]->run;
pthread_t thread;
pthread_create(&thread, NULL, (void*(*)(void*))run, NULL);
}
}
The error that I get is the following:
error: lvalue required as unary ‘&’ operand
Edit: run() is a method from class RobotNodes.cpp that is included on the top of this class.
There seems to be a non-static member function in the class RobotNodes
and you seem to think that the type of member function is void (*)()
. If so, then you are wrong.
The type of non-static member functon and free function are not same, even if they have exactly same signature!
So I would suggest you to define a static function called start
, as:
class RobotNodes
{
public:
void run(); //define it as you like
static void* start(void *pdata)
{
RobotNodes *robot = static_cast<RobotNodes*>(pdata);
robot->run(); //forward it
return 0; //or return as the documentation says
}
};
Then use start
as :
std::vector<pthread_t> threads(numRobotsToInit);
for(i = 0; i<numRobotsToInit; i++)
{
pthread_create(&threads[i], NULL, RobotNodes::start, &robots[i]);
}
Also, notice that I have created a vector of pthread_t
outside the loop; it is because each thread instance has to be different if they are different thread, and furthermore, each thread instance must continue to exist even after the loop stops.
这篇关于pthread_create参数函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!