Boost library ASIO io_service and run, Run_one, poll, poll_one differences

Source: Internet
Author: User

First, the role of Io_service

Io_servie implements a task queue, where the task is a void (void) function. Io_servie most commonly used two interfaces are post and run,post to the task queue, run is the task in the execution queue until all is done, and run can be called by n threads. Io_service is a fully thread-safe queue.

Second, the Io_servie interface

The interfaces provided are run, Run_one, poll, Poll_one, stop, Reset, dispatch, post, most commonly run, post, stop

Third, Io_servie implements the basic class structure of the code:

Io_servie is the interface class, for the implementation of cross-platform, the adoption of a policy model, all interfaces have Impl_type implementation. Depending on the platform different impl_type is divided into

Win_iocp_io_service win version of the implementation, here the main analysis of the Linux version.

Task_io_service the implementation under the non-win platform, its code structure is:

DETAIL/TASK_IO_SERVICE_FWD.HPP Simple Declaration Task_io_service name

Detail/task_io_service.hpp declaring methods and properties of Task_io_service

Detail/impl/task_io_service.ipp Specific implementation files

The task type in the queue is Opertioan, and the prototype is actually a typedef task_io_service_operation operation, which implements the file in Detail/task_io_service_ OPERATION.HPP, when a task in a queue is executed, it is task_io_service_operation:: When complete is called.

Iv.the difference between dispatch and post

The post must be postqueuedcompletionstatus and executed after GetQueuedCompletionStatus.

Dispatch will first check that the current thread is not a io_service.run/runonce/poll/poll_once thread, and if so, run it directly.

The realization of Io_servie::run method

The Run method executes all the tasks in the queue until the task has finished executing.

The Run method first constructs a idle_thread_info, which is the same as the First_idle_thread_ type, which concatenates all the threads through First_idle_thread_, which is not concatenated immediately. When the thread does not have a task to do is to join to the First_idle_thread_ header, when there is a task to execute, from the First_idle_thread_ break open. This is normal because First_idle_thread_ maintains the current idle thread.

Lock, Loop to execute the Do_one method until Do_one returns false

Do_one performs one task at a time. First check whether the queue is empty, if NULL appends this thread to the header of First_idle_thread_, and then blocks on the condition variable until it wakes up.

When awakened or executed for the first time, if Stopped_ is true (that is, the Stop method is called at this time), returns 0

Queue non-empty, pop out a task, check queue no task so simple unlock, if still have, call Wake_one_thread_and_unlock try to wake up other idle thread execution. It then executes the task, returning 1.

In fact, there is a special judgment if (o ==&task_operation_) when executing the queue task, then the TASK_->RUN,TASK_ variable type reactor will be executed, and the Linux platform is implemented as Epoll_reactor, The implementation of the code file for the Detail/impl/epoll_reactor.ipp,run method is actually performed by Epoll_wait,run blocking on epoll_wait waiting for the event to arrive, and the function that will require a callback after the event is processed to push to Io_ Servie's task queue, although epoll_wait is blocked, but it provides the interrupt function, how the interrupt is implemented, it adds a file descriptor to epoll_wait, which has 8 bytes in the file descriptor, This file descriptor is dedicated to interrupt epoll_wait, he is encapsulated in Select_interrupter, Select_interrupter actually implemented is Eventfd_select_interrupter, At the time of construction, two file descriptors are created by the pipe system call, then 8 bytes are written in advance via WRITE_FD, and the 8 bytes are retained. The Epollet level trigger is used in the add to epoll_wait, so that as long as the Select_interrupter read file descriptor is added to the epoll_wait, the epoll_wait is immediately interrupted.

The principle of the Run method is:

Have a task to perform the task immediately and try to get all the threads to perform the task together

If there is no task, block waiting for IO event on epoll_wait

If a new task arrives and there is no idle thread, break epoll_wait first and perform the task first

If there is a task in the queue and a Epoll_wait listener event is required, the non-blocking call epoll_wait (the timeout field is set to 0) will be blocked on the epoll_wait until the task is executed.

The use of the thread almost reached the extreme.

From this function can be known, when using ASIO, Io_servie should be as many as possible, so that it can epoll_wait occupy the most time slices, so as to maximize the response to IO events, reduce response latency. But each io_servie::run occupies one thread, so the Io_servie best should be the same as the CPU's number of cores.

VI. realization of Io_servie::stop

Lock, call Stop_all_threads

Set the Stopped_ variable to true to traverse all idle threads, and then wake up

Task_interrupted_ is set to True to call Task_ 's Interrupt method. <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.