![](/img/trans.png)
[英]Start thread within member function using std::thread & std::bind
[英]how to convert to "C" format by using std::bind member function of a class as a thread portal function?
如果有线程创建功能
int thread_create( void*(*create_cb)(void*), void* arg);
有一个具有成员函数的类示例:
void* example::startupTask(void *args);
如何通过使用类的 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);
此代码可以运行崩溃。
不可能的。 std::bind
返回一个带有operator()
的 CPP 对象,所以它就像一个函数。 thread_create
需要一个指向内存地址的指针。
相反,您应该使用thread_create
的附加参数来传递函数的上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.