boost::asio::io_service
/// Provides core I/O functionality.
/**
* The io_service class provides the core I/O functionality for users of the
* asynchronous I/O objects, including:
* io_service类为下面的异步对象提供了核心的I/O操作函数
*
* @li boost::asio::ip::tcp::socket
* @li boost::asio::ip::tcp::acceptor
* @li boost::asio::ip::udp::socket
* @li boost::asio::deadline_timer.
*
* The io_service class also includes facilities intended for developers of
* custom asynchronous services.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Safe, with the specific exceptions of the reset() and
* notify_fork() functions. Calling reset() 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.
* 当run、run_one、poll、poll_one这些函数没有完成时,调用reset会引起未定义的行为。
* 如果有其它进程正在调用io_service的任何函数或者任何I/O对象的函数(这些I/O对象与
* io_service有关),则一定不要调用notify_fork()函数。
*
* @par Concepts:
* Dispatcher.
*
* @par Synchronous and asynchronous operations
*
* 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.
* 在I/O对象上的同步操作会隐式的调用io_service对象。在c++程序中,io_service如果要完成异步操作,必须
* 调用run、run_one、poll、poll_one。 异步操作完成是指分发调用相关的处理句柄。
*
* @par Effect of exceptions thrown from handlers
*
* 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.
* 假如一个回调句柄抛出异常,这个异常可以传播给那些调用run、run_one、poll、pool_one
* 的进程,其它没有调用这些函数的进程不受影响。然后应用程序有责任捕捉这个异常。
*
* After the exception has been caught, the run(), run_one(), poll() or
* poll_one() call may be restarted @em without the need for an intervening
* call to reset(). This allows the thread to rejoin the io_service object's
* thread pool without impacting any other threads in the pool.
* 捕捉异常后,run、run_one、poll、poll_one可以被重新调用,不需要再调用reset。
* 这允许进程重新加入io_service对象的进程池,从而不影响其他已经在进程池中的进程。
* For example:
*
* @code
* boost::asio::io_service io_service;
* ...
* for (;;)
* {
* try
* {
* io_service.run();
* break; // run() exited normally
* }
* catch (my_exception& e)
* {
* // Deal with exception as appropriate.
* }
* }
* @endcode
*
* @par Stopping the io_service from running out of work
*
* 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 boost::asio::io_service::work:
* //add by wyp// work类可以阻止io_service的run函数在无任何任务时返回。这在后台服务的进程
* 中经常用到。
*
* @code boost::asio::io_service io_service;
* boost::asio::io_service::work work(io_service);
* ... @endcode
*
* 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.
* //stop()函数可以停止io_service,这将导致run()函数立即返回,那些未完成的操作被终止,
* 那些满足条件的句柄不能被分发。
*
* Alternatively, if the application requires that all operations and handlers
* be allowed to finish normally, the work object may be explicitly destroyed.
*
* @code boost::asio::io_service io_service;
* auto_ptr<boost::asio::io_service::work> work(
* new boost::asio::io_service::work(io_service));
* ...
* work.reset(); // Allow run() to exit. @endcode
*
* @par The io_service class and I/O services
*
* Class io_service implements an extensible, type-safe, polymorphic set of I/O
* services, indexed by service type. An object of class io_service must be
* initialised before I/O objects such as sockets, resolvers and timers can be
* used. These I/O objects are distinguished by having constructors that accept
* an @c io_service& parameter.
* io_service实现了可扩张的、类型安全的、多态的I/O服务集,按服务类型索引。在使用I/O对象
* 之前,必须初始化一个io_service对象。I/O对象通过接受一个io_service对象的引用区分。
*
* I/O services exist to manage the logical interface to the operating system on
* behalf of the I/O objects. In particular, there are resources that are shared
* across a class of I/O objects. For example, timers may be implemented in
* terms of a single timer queue. The I/O services manage these shared
* resources.
*
* Access to the services of an io_service is via three function templates,
* use_service(), add_service() and has_service().
*
* In a call to @c use_service<Service>(), the type argument chooses a service,
* making available all members of the named type. If @c Service is not present
* in an io_service, an object of type @c Service is created and added to the
* io_service. A C++ program can check if an io_service implements a
* particular service with the function template @c has_service<Service>().
* 通过调用use_service,类型参数选择一个Service,这种类型的所有成员就可以使用了。
* 如果Service不在一个io_service,就会创建一个type的Service,并把这个Service加入到io_service。
* 在c++程序中,我们可以用has_service来检查一个io_service对象是否存在一个特殊的service。
*
* Service objects may be explicitly added to an io_service using the function
* template @c add_service<Service>(). If the @c Service is already present, the
* service_already_exists exception is thrown. If the owner of the service is
* not the same object as the io_service parameter, the invalid_service_owner
* exception is thrown.
* Service 对象可以通过add_service加入io_service对象中,加入这个Service对象已经存在,
* 会抛出一个service已经存在的异常。 如果service的拥有者不和参数io_service是同一个对象,
* 会抛出无效的service拥有者的异常。
*
* Once a service reference is obtained from an io_service object by calling
* use_service(), that reference remains usable as long as the owning io_service
* object exists.
* 一旦通过调用use_service获得一个service的引用,那么这个引用一直有效,只要这个service的
* 拥有者存在。
*
* All I/O service implementations have io_service::service as a public base
* class. Custom I/O services may be implemented by deriving from this class and
* then added to an io_service using the facilities described above.
*/
class io_service
: private noncopyable
{
private:
typedef detail::io_service_impl impl_type;
#if defined(BOOST_ASIO_HAS_IOCP)
friend class

Boost.Asio库中的io_service是异步操作的核心,它管理着各种I/O对象,如TCP套接字、UDP套接字、定时器等。io_service提供线程安全的执行环境,处理异步操作的调度和执行。通过工作对象(work)可以防止io_service在无任务时退出,而reset()和stop()用于控制io_service的运行状态。此外,io_service还支持自定义服务的添加和管理,允许扩展其功能。
最低0.47元/天 解锁文章
272

被折叠的 条评论
为什么被折叠?



