简体   繁体   English

如何通过使用类的 std::bind 成员函数作为线程门户函数转换为“C”格式?

[英]how to convert to "C" format by using std::bind member function of a class as a thread portal function?

if there is function for thread creating如果有线程创建功能

int thread_create( void*(*create_cb)(void*), void* arg);

there is a class example which have a member function:有一个具有成员函数的类示例:

void* example::startupTask(void *args);

how to convert to "C" format by using std::bind member function of a class as a thread portal function?如何通过使用类的 std::bind 成员函数作为线程门户函数转换为“C”格式?

std::function<void* (void*)> func = std::bind(&example::startupTask, this, std::placeholders::_1);
auto rtn = thread_create((*func.target<std::_Bind <void*(example::*(example*, std::_Placeholder<1>))(void*)> >()), nullptr);

this code can run crashed.此代码可以运行崩溃。

Impossible.不可能的。 std::bind returns a CPP object with operator() , so it acts like a function. std::bind返回一个带有operator()的 CPP 对象,所以它就像一个函数。 thread_create expects a pointer to a memory address. thread_create需要一个指向内存地址的指针。

Instead, you should use the additional parameter in thread_create to pass the context for the function.相反,您应该使用thread_create的附加参数来传递函数的上下文。

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

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