Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Fork

Boost.Asio supports programs that utilise the fork() system call. Provided the program calls io_context.notify_fork() at the appropriate times, Boost.Asio will recreate any internal file descriptors (such as the "self-pipe trick" descriptor used for waking up a reactor). The notification is usually performed as follows:

io_context_.notify_fork(boost::asio::io_context::fork_prepare);
if (fork() == 0)
{
  io_context_.notify_fork(boost::asio::io_context::fork_child);
  ...
}
else
{
  io_context_.notify_fork(boost::asio::io_context::fork_parent);
  ...
}

User-defined services can also be made fork-aware by overriding the io_context::service::notify_fork() virtual function.

Note that any file descriptors accessible via Boost.Asio's public API (e.g. the descriptors underlying basic_socket<>, posix::stream_descriptor, etc.) are not altered during a fork. It is the program's responsibility to manage these as required.

See Also

io_context::notify_fork(), io_context::fork_event, io_context::service::notify_fork(), Fork examples.


PrevUpHomeNext