O'Reilly logo

Boost.Asio C++ Network Programming - Second Edition by John Torjo, Wisnu Anggoro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Timing the work execution using the timer class

There is a class in the Boost C++ library that provides the ability to conduct a blocking or asynchronous wait for a timer until it expires, known as the deadline timer. A deadline timer indicates one of two states: expired or not expired.

An expiring timer

Here, we are going to create a timer that will expire in 10 seconds. Let us take a look at the following code:

/* timer.cpp */ #include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream> boost::mutex global_stream_lock; void WorkerThread(boost::shared_ptr<boost::asio::io_service> iosvc, int counter) { global_stream_lock.lock(); std::cout << "Thread ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required