简体   繁体   English

来自非静态成员函数的pthread_create

[英]pthread_create from non-static member function

This is somewhat similiar to this : pthread function from a class 这有点类似于: 类中的pthread函数

But the function that's getting called in the end is referencing the this pointer, so it cannot be made static. 但是最后被调用的函数正在引用this指针,因此不能将其设为静态。

void * Server::processRequest()
{
     std::string tmp_request, outRequest;
     tmp_request = this->readData();
     outRequest = this->parse(tmp_request);
     this->writeReply(outRequest);
}

void * LaunchMemberFunction(void * obj)
{
    return ((Server *)obj)->processRequest();
}

and then the pthread_create 然后是pthread_create

Server SServer(soc);

pthread_create(&handler[tcount], &attr, (void*)LaunchMemberFunction,(void*)&SServer);

errors: 错误:

SS_Twitter.cpp:819: error: invalid conversion from void* to void* ( )(void ) SS_Twitter.cpp:819: error: initializing argument 3 of int pthread_create(pthread_t*, const pthread_attr_t*, void* ( )(void ), void*) SS_Twitter.cpp:819:错误:从void *到void *( 无效转换( )(void )SS_Twitter.cpp:819:错误:初始化int pthread_create(pthread_t *,const pthread_attr_t *,void *( )(void )的参数3 ,无效*)

You are casting the third argument to a void* ((void*) , and then getting an error, as void* cannot be cast to a function pointer. 您正在将第三个参数转换为void* ((void*) ,然后得到一个错误,因为void*无法转换为函数指针。

I believe it should compile if you just use &LaunchMemberFunction instead. 我相信如果您只使用&LaunchMemberFunction代替,它应该可以编译。

get rid of the (void*) in front of LaunchMemberFunction. 摆脱LaunchMemberFunction前面的(void *)。 It's both unecessary and incorrect, and the cause of your error. 这既不必要又不正确,也是造成错误的原因。 The language will not implicitly convert from void * to a pointer to function type, which would be needed for the code you have written to work. 该语言不会从void *隐式转换为指向函数类型的指针,这对于您编写的代码来说是必需的。 That argument to pthread_create is already the right type, there is no reason to cast it to something else. pthread_create的参数已经是正确的类型,没有理由将其强制转换为其他类型。

In your definition of function I don't see input argument type please use as the following 在您定义的函数中,我看不到输入参数类型,请按以下方式使用

void * Server::processRequest(void)
{
     std::string tmp_request, outRequest;
     tmp_request = this->readData();
     outRequest = this->parse(tmp_request);
     this->writeReply(outRequest);
}

Second thing you do not have to case a function pointer just use as the following 第二件事,您不必使用函数指针,只需使用以下代码

pthread_create(&handler[tcount], &attr, &LaunchMemberFunction,(void*)&SServer);

The problem is with the cast. 问题出在演员表上。 You are casting the function pointer to a void* that can not be implicitly converted to ( void (*)(void*) --which is the type accepted by pthread_create for the third argument. 您正在铸造函数指针到一个void*不能被隐式转换为( void (*)(void*)可呈现是接受了类型pthread_create为第三个参数。

You do not need to cast the argument as it is of the exact type, but if you had to, it would be: 您不必强制转换参数的类型,因为它是确切的类型,但是如果需要,它将是:

void LaunchMemberFunction( Server * ); // signature now does not match, cast required:

static_cast<void (*)(void*)>(LaunchMemberFunction)
// or with C casts:
(void (*)(void*))LaunchMemberFunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM