asio C++ library

PrevUpHomeNext

io_service::notify_fork

Notify the io_service of a fork-related event.

void notify_fork(
    asio::io_service::fork_event event);

This function is used to inform the io_service that the process is about to fork, or has just forked. This allows the io_service, and the services it contains, to perform any necessary housekeeping to ensure correct operation following a fork.

This function must not be called while any other io_service function, or any function on an I/O object associated with the io_service, is being called in another thread. It is, however, safe to call this function from within a completion handler, provided no other thread is accessing the io_service.

Parameters

event

A fork-related event.

Exceptions

asio::system_error

Thrown on failure. If the notification fails the io_service object should no longer be used and should be destroyed.

Example

The following code illustrates how to incorporate the notify_fork() function:

my_io_service.notify_fork(asio::io_service::fork_prepare);
if (fork() == 0)
{
  // This is the child process.
  my_io_service.notify_fork(asio::io_service::fork_child);
}
else
{
  // This is the parent process.
  my_io_service.notify_fork(asio::io_service::fork_parent);
}
Remarks

For each service object svc in the io_service set, performs svc->fork_service();. When processing the fork_prepare event, services are visited in reverse order of the beginning of service object lifetime. Otherwise, services are visited in order of the beginning of service object lifetime.


PrevUpHomeNext