Yes, io_service::post()
is a convenient facility to post a functor from one thread to another, but the destination thread should execute io_service::run()
, which is blocking function (it's kind of io_service
"message loop"). So, assuming your main thread looks like this:
int main()
{
// do some preparations, launch other threads...
// ...
io_service io;
io.run();
}
...and assuming you've got an access to io
object from pollService
running in another thread, you can do the following:
SomeClass::pollService()
{
// do something...
// ...
io.post([=] { doStuffThatShoudRunInMainThread(); });
}
If your compiler doesn't support c++11 lambdas, use bind
-- but note that post
expects nullary functor, i.e. a function-object that doesn't accept parameters.