我想在 pthread 中运行回调函数。
我目前卡在以下代码中:
//maintest.cpp
....
main{
...
//setting up the callback function SimT:
boost::asio::io_service io;
boost::asio::deadline_timer t(io);
SimT d(t);
//calling io.run() in another thread with io.run()
pthread_t a;
pthread_create( &a, NULL, io.run(),NULL); ----->Here I dont know how to pass the io.run() function
...
//other stuff that will be executed during io.run()
}
我应该如何在 pthread_create 参数中指定 io.run()? 谢谢
最佳答案
您需要将指针传递给非成员函数,例如:
extern "C" void* run(void* io) {
static_cast<io_service*>(io)->run();
return nullptr; // TODO report errors
}
pthread_create(&a, nullptr, run, &io);
当然,现在已经没有必要乱搞原生线程库了:
std::thread thread([&]{io.run();});
关于c++ pthread : how to set io_service. run() 作为 pthread_create 中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19814728/