Provides core I/O functionality.
class io_service : public execution_context
Name |
Description |
---|---|
Executor used to submit functions to an io_service. |
|
Base class for all io_service services. |
|
Provides serialised handler execution. |
|
(Deprecated: Use executor_work.) Class to inform the io_service when it has work to do. |
|
Fork-related event notifications. |
Name |
Description |
---|---|
(Deprecated: Use asio::dispatch().) Request the io_service to invoke the given handler. |
|
Obtains the executor associated with the io_service. |
|
Constructor. |
|
Notify the execution_context of a fork-related event. |
|
Run the io_service object's event processing loop to execute ready handlers. |
|
Run the io_service object's event processing loop to execute one ready handler. |
|
(Deprecated: Use asio::post().) Request the io_service to invoke the given handler and return immediately. |
|
(Deprecated: Use restart().) Reset the io_service in preparation for a subsequent run() invocation. |
|
Restart the io_service in preparation for a subsequent run() invocation. |
|
Run the io_service object's event processing loop. |
|
Run the io_service object's event processing loop to execute at most one handler. |
|
Stop the io_service object's event processing loop. |
|
Determine whether the io_service object has been stopped. |
|
(Deprecated: Use asio::wrap().) Create a new handler that automatically dispatches the wrapped handler on the io_service. |
|
Destructor. |
Name |
Description |
---|---|
Destroys all services in the context. |
|
Shuts down all services in the context. |
Name |
Description |
---|---|
(Deprecated: Use make_service().) Add a service object to the execution_context. |
|
Determine if an execution_context contains a specified service type. |
|
Creates a service object and adds it to the execution_context. |
|
Obtain the service object corresponding to the given type. |
The io_service
class provides the core I/O functionality for users of the asynchronous I/O
objects, including:
asio::ip::tcp::socket
asio::ip::tcp::acceptor
asio::ip::udp::socket
deadline_timer
.
The io_service
class also includes facilities intended for developers of custom asynchronous
services.
Distinct objects: Safe.
Shared objects: Safe, with the
specific exceptions of the restart()
and notify_fork()
functions. Calling restart()
while there are unfinished run()
,
run_one()
,
poll()
or poll_one()
calls results in undefined behaviour. The notify_fork()
function should not be called while any
io_service
function, or any function on an I/O object that is associated with the io_service
,
is being called in another thread.
Synchronous operations on I/O objects implicitly run the io_service
object for an individual
operation. The io_service
functions run()
,
run_one()
,
poll()
or poll_one()
must be called for the io_service
to perform asynchronous
operations on behalf of a C++ program. Notification that an asynchronous
operation has completed is delivered by invocation of the associated handler.
Handlers are invoked only by a thread that is currently calling any overload
of run()
,
run_one()
,
poll()
or poll_one()
for the io_service
.
If an exception is thrown from a handler, the exception is allowed to propagate
through the throwing thread's invocation of run()
, run_one()
, poll()
or poll_one()
. No other threads that are calling any
of these functions are affected. It is then the responsibility of the application
to catch the exception.
After the exception has been caught, the run()
, run_one()
, poll()
or poll_one()
call may be restarted without
the need for an intervening call to restart()
. This allows the thread to rejoin the
io_service
object's thread pool without impacting any other threads in the pool.
For example:
asio::io_service io_service; ... for (;;) { try { io_service.run(); break; // run() exited normally } catch (my_exception& e) { // Deal with exception as appropriate. } }
Some applications may need to prevent an io_service
object's run()
call
from returning when there is no more work to do. For example, the io_service
may be being run in a
background thread that is launched prior to the application's asynchronous
operations. The run()
call may be kept running by creating an object of type io_service::work
:
asio::io_service io_service; asio::io_service::work work(io_service); ...
To effect a shutdown, the application will then need to call the io_service
object's stop()
member function. This will cause the io_service
run()
call to return as soon as possible, abandoning
unfinished operations and without permitting ready handlers to be dispatched.
Alternatively, if the application requires that all operations and handlers be allowed to finish normally, the work object may be explicitly destroyed.
asio::io_service io_service; auto_ptr<asio::io_service::work> work( new asio::io_service::work(io_service)); ... work.reset(); // Allow run() to exit.
Header: asio/io_service.hpp
Convenience header: asio.hpp