Given a boost::asio::io_service io
is it safe to call io.post(...)
on threads other than the thread that started io.run()
?
For example:
boost::asio::io_service io;
void f()
{
/* do something */
io.post(&f);
}
void g()
{
/* do something else */
io.post(&g)
}
int main()
{
std::thread t1(&f);
std::thread t2(&g);
io.run();
t1.join();
t2.join();
return 0;
}
I assume that io_service
uses some kind of internal data structure (e.g. a queue) and posting alters this data structure (e.g. pushing onto the queue). My concern is that the data structure may or may not be thread-safe.
I've searched around and haven't been able to find a straight answer to this question, although everything I've seen seems to indicate that post()
is thread-safe (i.e. atomic). Can someone please verify?