C + + Timers

Source: Internet
Author: User

Io_service's Task execution process:
Call the Run method and enter the main loop;
Determines whether the public queue is empty, does not empty the task and executes, and wakes up other idle threads when the number of tasks is greater than 1 o'clock;
The task execution ends and the tasks within the private team of each thread are moved to the public task queue.
Trigger Reactor,linux The following is generally epoll, when there is an event, put the task of the corresponding event into the private queue.
When the queue is empty, the current thread is added to the idle thread queue, while entering the wait state, waiting for other threads to wake up.
When the user calls post, the task is delivered directly to the public queue.

Io_service.run (): Starts execution of all tasks in the queue until the task is completed.

Sync Timer:

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ Time.hpp>int Main () {boost::asio::io_service iOS;//All ASIO programs must have a Io_service object Boost::asio::d eadline_timer T (iOS , Boost::p osix_time::seconds (2)); Timers Std::cout << t.expires_at () << Std::endl; Check the absolute time of timer termination t.wait (); Sync wait std::cout << "hello!" << Std::endl;return 0;}

  

Asynchronous timers:

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ time.hpp>//Error_code indicates program run error void Call_back (const boost::system::error_code& e) {std::cout << "I am Call _back. "<< Std::endl;} int main () {boost::asio::io_service iOS;//All ASIO programs must have a Io_service object Boost::asio::d eadline_timer T (iOS, Boost:: Posix_time::seconds (2)); Timer t.async_wait (call_back); Asynchronous wait, incoming callback function Std::cout << "I'm in front of the timer" << Std::endl;ios.run (); Waits for the asynchronous operation to complete, Io_service gets the execution result from the operating system when it finishes, calls the completion handler return 0;}

  

Asynchronous timers using Bind

#include <boost/thread.hpp> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_ Time.hpp>class a_timer{private:int Count, count_max;boost::function<void () > F;//no parameter no return callable with Boost::asio:: Deadline_timer T; Timer public:template<typename F>a_timer (boost::asio::io_service &ios, int x, F func): F (func), Count_max (x), Count (0), T (iOS, boost::p osix_time::millisec) {t.async_wait (Boost::bind (&a_timer::call_func, this, _1)); Register callback function}void call_func (const Boost::system::error_code &e) {if (Count > Count_max) {return;} ++count;f (); T.expires_at (T.expires_at () + boost::p osix_time::millisec) t.async_wait (Boost::bind (&a_ Timer::call_func, this, _1)); Start the timer again}};void Print1 () {std::cout << "Hello print1." << Std::endl;} void Print2 () {std::cout << "Hello print2." << Std::endl;} int main () {boost::asio::io_service Ios;a_timer T1 (iOS, 5, print1); A_timer T2 (iOS, 5, Print2); Ios.run ();// Asynchronously waits for the call to end with return 0;}

  

C + + Timers

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.