| Home | Libraries | People | FAQ | More

# boost::asio::io_service Class Reference

Inherits noncopyable.

Inheritance diagram for boost::asio::io_service:

List of all members.

## Detailed Description

Provides core I/O functionality.

The io_service class provides the core I/O functionality for users of the asynchronous I/O objects, including:

- boost::asio::ip::tcp::socket
- boost::asio::ip::tcp::acceptor
- boost::asio::ip::udp::socket
- boost::asio::deadline_timer.

The io_service class also includes facilities intended for developers of custom asynchronous services.

Thread Safety:
  Distinct objects: Safe.
  Shared objects: Safe, with the exception that calling reset() while there are unfinished run() calls results in undefined behaviour.

Concepts:
  Dispatcher.

See also:
  Effect of exceptions thrown from handlers

## Public Member Functions

| io_service ()
| Default constructor.
void | run ()
| Run the io_service's event processing loop.
void | interrupt ()
| Interrupt the io_service's event processing loop.
void | reset ()
| Reset the io_service in preparation for a subsequent run() invocation.
template<typename Handler>
void | dispatch (Handler handler)
| Request the io_service to invoke the given handler.
template<typename Handler>
void | post (Handler handler)
| Request the io_service to invoke the given handler and return immediately.
template<typename Handler>
unspecified | wrap (Handler handler)
| Create a new handler that automatically dispatches the wrapped handler on the io_service.

## Friends

template<typename Service>
Service & | use_service (io_service &ios)
| Obtain the service object corresponding to the given type.
template<typename Service>
void | add_service (io_service &ios, Service *svc)
| Add a service object to the io_service.
template<typename Service>
bool | has_service (io_service &ios)
| Determine if an io_service contains a specified service type.

## Classes

class | service
| Base class for all io_service services. More...
class | work
| Class to inform the io_service when it has work to do. More...

## Constructor & Destructor Documentation

boost::asio::io_service::io_service | ( |  | ) |

Default constructor.

## Member Function Documentation

void boost::asio::io_service::run | ( |  | ) |

Run the io_service's event processing loop.

The run() function blocks until all work has finished and there are no more handlers to be dispatched, or until the io_service has been interrupted.

Multiple threads may call the run() function to set up a pool of threads from which the io_service may execute handlers.

The run() function may be safely called again once it has completed only after a call to reset().

void boost::asio::io_service::interrupt | ( |  | ) |

Interrupt the io_service's event processing loop.

This function does not block, but instead simply signals to the io_service that all invocations of its run() member function should return as soon as possible.

Note that if the run() function is interrupted and is not called again later then its work may not have finished and handlers may not be delivered. In this case an io_service implementation is not required to make any guarantee that the resources associated with unfinished work will be cleaned up.

void boost::asio::io_service::reset | ( |  | ) |

Reset the io_service in preparation for a subsequent run() invocation.

This function must be called prior to any second or later set of invocations of the run() function. It allows the io_service to reset any internal state, such as an interrupt flag.

This function must not be called while there are any unfinished calls to the run() function.

template<typename Handler>

void boost::asio::io_service::dispatch | ( | Handler | handler | ) |

Request the io_service to invoke the given handler.

This function is used to ask the io_service to execute the given handler.

The io_service guarantees that the handler will only be called in a thread in which the run() member function is currently being invoked. The handler may be executed inside this function if the guarantee can be met.

Parameters:

  | handler | The handler to be called. The io_service will make a copy of the handler object as required. The function signature of the handler must be:
       void handler();

template<typename Handler>

void boost::asio::io_service::post | ( | Handler | handler | ) |

Request the io_service to invoke the given handler and return immediately.

This function is used to ask the io_service to execute the given handler, but without allowing the io_service to call the handler from inside this function.

The io_service guarantees that the handler will only be called in a thread in which the run() member function is currently being invoked.

Parameters:

  | handler | The handler to be called. The io_service will make a copy of the handler object as required. The function signature of the handler must be:
       void handler();

template<typename Handler>

unspecified boost::asio::io_service::wrap | ( | Handler | handler | ) |

Create a new handler that automatically dispatches the wrapped handler on the io_service.

This function is used to create a new handler function object that, when invoked, will automatically pass the wrapped handler to the io_service's dispatch function.

Parameters:

  | handler | The handler to be wrapped. The io_service will make a copy of the handler object as required. The function signature of the handler must be:
       void handler(A1 a1, ... An an);

Returns:
  A function object that, when invoked, passes the wrapped handler to the io_service's dispatch function. Given a function object with the signature:

       R f(A1 a1, ... An an);

  If this function object is passed to the wrap function like so:

       io_service.wrap(f);

  then the return value is a function object with the signature

       void g(A1 a1, ... An an);

  that, when invoked, executes code equivalent to:

       io_service.dispatch(boost::bind(f, a1, ... an));

## Friends And Related Function Documentation

template<typename Service>

Service& use_service | ( | io_service & | ios | ) | `[friend]`

Obtain the service object corresponding to the given type.

This function is used to locate a service object that corresponds to the given service type. If there is no existing implementation of the service, then the io_service will create a new instance of the service.

Parameters:

  | ios | The io_service object that owns the service.

Returns:
  The service interface implementing the specified service type. Ownership of the service interface is not transferred to the caller.

template<typename Service>

void add_service | ( | io_service & | ios,
|  | Service * | svc |
| ) |  |  | `[friend]`

Add a service object to the io_service.

This function is used to add a service to the io_service.

Parameters:

  | ios | The io_service object that owns the service.
  | svc | The service object. On success, ownership of the service object is transferred to the io_service. When the io_service object is destroyed, it will destroy the service object by performing:
       delete static_cast<io_service::service*>(svc)

Exceptions:

  | boost::asio::service_already_exists | Thrown if a service of the given type is already present in the io_service.
  | boost::asio::invalid_service_owner | Thrown if the service's owning io_service is not the io_service object specified by the ios parameter.

template<typename Service>

bool has_service | ( | io_service & | ios | ) | `[friend]`

Determine if an io_service contains a specified service type.

This function is used to determine whether the io_service contains a service object corresponding to the given service type.

Parameters:

  | ios | The io_service object that owns the service.

Returns:
  A boolean indicating whether the io_service contains the service.

| Copyright © 2003 - 2006 Christopher M. Kohlhoff