C++ Boost ASIO simple periodic timer? - c++

I want a very simple periodic timer to call my code every 50ms. I could make a thread that sleeps for 50ms all the time (but that's a pain)... I could start looking into Linux API's for making timers (but it's not portable)...
I'd like to use boost.. I'm just not sure it's possible. Does boost provide this functionality?

A very simple, but fully functional example:
#include <iostream>
#include <boost/asio.hpp>
boost::asio::io_service io_service;
boost::posix_time::seconds interval(1); // 1 second
boost::asio::deadline_timer timer(io_service, interval);
void tick(const boost::system::error_code& /*e*/) {
std::cout << "tick" << std::endl;
// Reschedule the timer for 1 second in the future:
timer.expires_at(timer.expires_at() + interval);
// Posts the timer event
timer.async_wait(tick);
}
int main(void) {
// Schedule the timer for the first time:
timer.async_wait(tick);
// Enter IO loop. The timer will fire for the first time 1 second from now:
io_service.run();
return 0;
}
Notice that it is very important to call expires_at() to set a new expiration time, otherwise the timer will fire immediately because it's current due time already expired.

The second example on Boosts Asio tutorials explains it.
You can find it here.
After that, check the 3rd example to see how you can call it again with a periodic time intervall

To further expand on this simple example. It will block the execution as was said in the comments, so if you want more io_services running, you should run them in a thread like so...
boost::asio::io_service io_service;
boost::asio::io_service service2;
timer.async_wait(tick);
boost::thread_group threads;
threads.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
service2.run();
threads.join_all();

As I had some issues with prior answers, here is my example:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
void print(const boost::system::error_code&, boost::asio::deadline_timer* t,int* count)
{
if (*count < 5)
{
std::cout << *count << std::endl;
++(*count);
t->expires_from_now(boost::posix_time::seconds(1));
t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count));
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count));
io.run();
std::cout << "Final count is " << count << std::endl;
return 0;
}
it did what it supposed to do: counting to five. May it help someone.

A Boost Asio add-on class that encapsulates this functionality (call a specfied function every N milliseconds): https://github.com/mikehaben69/boost/tree/main/asio
The repo includes a demo source file and makefile.

Related

Boost deadline timer fires continuously after IO service restart

I'm working on some comms code for which I need a regular timer for monitoring. I'm using boost ASIO for the comms so I decided to use a deadline timer for the timing and put it on the same IO service.
Everything works great the first time the code is run but it goes wrong once the comms (in my case a serial port) is stopped and restarted. The timer begins to malfunction and the comms break down. I believe the two are related so I'm just focusing on the timer for this question.
Consider the code below. This should start a timer and let it run for 10 seconds, stop the timer, and then start it again for another 10 seconds. What actually happens though is that when the timer is restarted it fires continuously, ie without any delay between firing.
#include <iostream>
#include <thread>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
boost::posix_time::ptime timer_start_;
void CallbackTimerFunc(boost::asio::deadline_timer* timer) {
auto time_since_start = timer->expires_at() - timer_start_;
std::cout << "It's been " << time_since_start.total_seconds() << " seconds." << std::endl;
// Sleep is here to prevent spamming when timer starts malfunctioning.
usleep(20000);
timer->expires_at(timer->expires_at() + boost::posix_time::milliseconds(1000));
timer->async_wait(boost::bind(&CallbackTimerFunc, timer));
}
int main(int /*argc*/, char** /*args*/) {
// Start
boost::asio::io_service io_service_;
boost::asio::deadline_timer deadline_timer_(io_service_);
deadline_timer_.expires_from_now(boost::posix_time::milliseconds(1000));
timer_start_ = deadline_timer_.expires_at();
deadline_timer_.async_wait(boost::bind(&CallbackTimerFunc, &deadline_timer_));
std::thread io_thread_(boost::bind(&boost::asio::io_service::run, &io_service_));
// Stop
sleep(10);
io_service_.stop();
while (!io_service_.stopped()) usleep(10000);
deadline_timer_.cancel();
io_thread_.join();
std::cout << "******************************" << std::endl;
// Restart
io_service_.restart();
deadline_timer_.expires_from_now(boost::posix_time::milliseconds(1000));
timer_start_ = deadline_timer_.expires_at();
deadline_timer_.async_wait(boost::bind(&CallbackTimerFunc, &deadline_timer_));
io_thread_ = std::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
// Stop
sleep(10);
io_service_.stop();
while (!io_service_.stopped()) usleep(10000);
deadline_timer_.cancel();
io_thread_.join();
return 0;
}
Expected output is for the timer to count to 10 (well in reality from 0 to 8) twice. The actual output is that it counts to 10 once, and then just goes haywire claiming that hundreds of seconds are passing.
I can make this code work by creating a brand new IO service and timer but that seems like it should be unnecessary given that they are supposed to be reusable.
If anyone can tell me what's going on here or at least reproduce my results I'd appreciate it.
Thanks to #tkausl for putting me on the right track. Here is the corrected code. Note the extra check at the top of CallbackTimerFunc.
#include <iostream>
#include <thread>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
boost::posix_time::ptime timer_start_;
void CallbackTimerFunc(const boost::system::error_code& error, boost::asio::deadline_timer* timer) {
if (error.value() == boost::asio::error::operation_aborted) {
std::cout << "Abort was sent on the first firing. Because of course it would be. Ignoring it will fix the problem. Because of course it will." << std::endl;
return;
}
auto time_since_start = timer->expires_at() - timer_start_;
std::cout << "It's been " << time_since_start.total_seconds() << " seconds." << std::endl;
// Sleep is here to prevent spamming when timer starts malfunctioning.
usleep(20000);
timer->expires_at(timer->expires_at() + boost::posix_time::milliseconds(1000));
timer->async_wait(boost::bind(&CallbackTimerFunc, boost::asio::placeholders::error, timer));
}
int main(int /*argc*/, char** /*args*/) {
// Start
boost::asio::io_service io_service_;
boost::asio::deadline_timer deadline_timer_(io_service_);
deadline_timer_.expires_from_now(boost::posix_time::milliseconds(1000));
timer_start_ = deadline_timer_.expires_at();
deadline_timer_.async_wait(boost::bind(CallbackTimerFunc, boost::asio::placeholders::error, &deadline_timer_));
std::thread io_thread_(boost::bind(&boost::asio::io_service::run, &io_service_));
// Stop
sleep(10);
io_service_.stop();
while (!io_service_.stopped()) usleep(10000);
deadline_timer_.cancel();
io_thread_.join();
std::cout << "******************************" << std::endl;
// Restart
io_service_.restart();
deadline_timer_.expires_from_now(boost::posix_time::milliseconds(1000));
timer_start_ = deadline_timer_.expires_at();
deadline_timer_.async_wait(boost::bind(CallbackTimerFunc, boost::asio::placeholders::error, &deadline_timer_));
io_thread_ = std::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
// Stop
sleep(10);
io_service_.stop();
while (!io_service_.stopped()) usleep(10000);
deadline_timer_.cancel();
io_thread_.join();
return 0;
}

In boost::asio is it possible to have one deadline_timer with multiple execution times?

So I was experimenting with the deadline_timer class and wrote the code below to see if I could have on deadline_timer with multiple async_wait operations that would execute at different times.
Below I create a deadline timer in the main function all the way at the bottom and initially set it to expire after 3 seconds. Then I call an async_wait operation and pass the first print function as the handler. I then use the expires_from_now operation to set the time of expiration for what I intended to only affect the second async_wait call which has print2 as a handler. The output from running this is below the code.
This is test1.cpp
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/bind.hpp>
#include <time.h>
#include <sys/time.h>
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// Handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
void print(double start, const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!" << std::endl;
std::cout << get_wall_time() - start << std::endl;
}
void print2(double start, const boost::system::error_code& /*e*/)
{
std::cout << "SECOND Hello, world!" << std::endl;
std::cout << get_wall_time() - start << std::endl;
}
int main(int argc, char* argv[])
{
boost::asio::io_service io;
boost::asio::deadline_timer timer(io, boost::posix_time::seconds(3));
auto start = get_wall_time();
timer.async_wait(boost::bind(print, start, boost::asio::placeholders::error));
timer.expires_from_now(boost::posix_time::seconds(20));
timer.async_wait(boost::bind(print2, start, boost::asio::placeholders::error));
io.run();
return 0;
}
Here is the output
Hello, world!
0.000774145
SECOND Hello, world!
20.0085
This is the output after commenting out the second async_wait with the expiration modification.
Hello, world!
3.00079
As you can see the first handler executes instantly when I intended for it to execute after 3 seconds. The second handler correctly executes after 20 seconds. Is there any way I could get the behavior I intended with a deadline_timer without having to create a bunch of them?
A timer must have only one outstanding async_wait at a time. IIRC, issuing another implicitly cancels the first one (which will fire it's handler with an error code) as if you called cancel() followed by async_wait().
If you want to respond to 2 timer events, you have 2 choices. Either have 2 timers, or set the timeout and issue the second async_wait in the handler of the first.

boos::asio async_wait seems to be blocking

I was learning boost asio documentation.I came across this deadline_timer example.
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
/*This timer example shows a timer that fires once every second.*/
void print(const boost::system::error_code& e, boost::asio::deadline_timer* t, int* count)
{
if (*count < 5)
{
std::cout << *count << std::endl;
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(print,boost::asio::placeholders::error, t, count));
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(10));
auto myfunc = boost::bind(print, boost::asio::placeholders::error, &t ,&count);
t.async_wait(myfunc);
std::cout << "async wait " << std::endl;
io.run();
std::cout << "Just called io.run() " << std::endl;
std::cout << "Final count is " << count << std::endl;
return 0;
}
The async_wait() function seems to be blocking (i.e waiting for the 10 second timer to expire)
The output from the above program is as follows.
async wait
0
1
2
3
4
Just called io.run()
Final count is 5
I would expect an async_wait() to create a separate thread and wait for the timer to expire there meanwhile executing the main thread.
i.e I would expect the program to print
Just called io.run()
Final count is 5
while waiting for the timer to expire.? Is my understanding wrong?
This is my understanding of async_wait(). This implementation looks more like a blocking wait. Is my understanding wrong? What am I missing?
The io.run(); statement is the key to explaining the difference between the output you're getting and the output you're expecting.
In the ASIO framework, any asynchronous commands need to have a dedicated thread to run the callbacks upon. But because ASIO is relatively low-level, it expects you to provide the thread yourself.
As a result, what you're doing when you call io.run(); within the main thread is to specify to the framework that you intend to run all asynchronous commands on the main thread. That's acceptable, but that also means that the program will block on io.run();.
If you intend the commands to run on a separate thread, you'll have to write something like this:
std::thread run_thread([&]() {
io.run();
});
std::cout << "Just called io.run() " << std::endl;
std::cout << "Final count is " << count << std::endl;
run_thread.join();
return 0;
The async_wait function isn't blocking, run is. That's run's job. If you don't want a thread to block in the io_service's processing loop, don't have that thread call run.
The async_wait function doesn't create any threads. That would make it expensive and make it much harder to control the number of threads servicing the io_service.
Your expectation is unreasonable because returning from main terminates the process. So who or what would wait for the timer?

How to delay a function without stopping the application

void sendCommand(float t,char* cmd)
{
std::clock_t endwait;
double endwait = clock () + t * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
if( clock() < endwait)
printf("\nThe waited command is =%s",cmd);
}
void Main()
{
sendCommand(3.0,"Command1");
sendCommand(2.0,"Command2");
printf("\nThe first value")
return 0;
}
i want to delay a function but my application should keep on running.
In the above code i want The first value to be printed first.
than i want Command2 to be printed and Command1 should be the last to be printed.
I prefer std::async for this.
#include <chrono>
#include <thread>
#include <future>
#include <iostream>
void sendCommand(std::chrono::seconds delay, std::string cmd)
{
std::this_thread::sleep_for( delay );
std::cout << "\nThe waited command is =" << cmd;
}
int main()
{
auto s1 = std::async(std::launch::async, sendCommand, std::chrono::seconds(3),"Command1");
auto s2 = std::async(std::launch::async, sendCommand, std::chrono::seconds(2),"Command2");
std::cout << "\nThe first value" << std::flush;
s1.wait();
s2.wait();
return 0;
}
However, for a real design, I would create a scheduler (or preferably use an existing one) which manages a priority queue sorted by the delay time. Spawning a new thread for every command will quickly become a problem. Since you flagged the question for MS VIsual C++, take a look the PPL which implements task-based parallelism.
And as it a C++ question, I would stay away from the C stuff and not use printf, CLOCK_PER_SEC, char*, clock etc. You will quickly get into problems even with this simple example when you start using strings instead of the "Command1" literals. std::string will help you here.
I think you need threads. You can do it like this:
#include <thread>
#include <chrono>
#include <iostream>
void sendCommand(float t, char const* cmd)
{
std::this_thread::sleep_for(std::chrono::milliseconds(int(t * 1000)));
std::cout << "\nThe waited command is = " << cmd << '\n';
}
int main()
{
// each function call on a new thread
std::thread t1(sendCommand, 3.0, "Command1");
std::thread t2(sendCommand, 2.0, "Command2");
// join the threads so we don't exit before they finish.
t1.join();
t2.join();
}
You can do it in many ways depending upon your actual logic.
Examles;:
1.you can you a global flag variable and check its state , when third print will complete you can set flag to 1,so next call will execute.
2.you can use a STACK .push all the function pointers in a STACK . and after that pop and execute.
3.MultiThreading. You can use it with proper synchronized way, but it will be complex. It depends upon your requirement.
Thanks !!!
boost::asio is nice because it doesn't require the overhead of multiple threads.
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
int main()
{
boost::asio::io_service svc;
boost::asio::deadline_timer t0{svc};
boost::asio::deadline_timer t1{svc};
boost::asio::deadline_timer t2{svc};
t0.expires_from_now(boost::posix_time::seconds{1});
t1.expires_from_now(boost::posix_time::seconds{2});
t2.expires_from_now(boost::posix_time::seconds{3});
t2.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t2!\n";});
t1.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t1!\n";});
t0.async_wait([] (const boost::system::error_code& ec) { if(!ec) std::cout << "Greetings from t0!\n";});
svc.post([] () { std::cout << "I'm number one!\n";});
svc.run();
return 0;
}
Gives the output:
I'm number one!
Greetings from t0!
Greetings from t1!
Greetings from t2!

boost::asio asynchronous timer as an interrupt

As I understand it, I should be able to use a boost:asio asynchronous timer to trigger a callback every n milliseconds whilst my program is doing something else without needing threads. Is that assumption correct ?
I put together the following test program which just prints the handler messages and never prints the rand() values. What I want is to see all the floating point numbers scroll down the screen, then every 250ms a handler message should appear in amongst them.
Here is the code :
#include <iostream>
#include <vector>
#include <cstdlib>
#include <boost/asio.hpp>
#include <boost/date_time.hpp>
#include <boost/thread.hpp>
boost::asio::io_service io_service;
boost::posix_time::time_duration interval(boost::posix_time::milliseconds(250));
boost::asio::deadline_timer timer(io_service,interval);
void handler(const boost::system::error_code& error);
void timer_init() {
timer.expires_at(timer.expires_at()+interval);
timer.async_wait(handler);
}
void handler(const boost::system::error_code& error) {
static long count=0;
std::cout << "in handler " << count++ << std::endl;
std::cout.flush();
timer_init();
}
int main(int argc, char **argv) {
timer.async_wait(handler);
io_service.run();
std::vector<double> vec;
for (long i=0; i<1000000000; i++) {
double x=std::rand();
std::cout << x << std::endl;
std::cout.flush();
vec.push_back(x);
}
return 0;
}
This:
io_service.run();
Is a blocking call. It's true that you can have multiple things happening asynchronously in one thread using ASIO, but you cannot have ASIO running in the same thread as code which is not integrated with ASIO. This is a classic event-driven model, where all the work gets done in response to some readiness notification (timers, in your case).
Try moving your vector/rand code to a function and passing that function to io_service::post(), which will then run that code within the context of its run() method. Then when you invoke run(), both things will happen (though not truly concurrently, as that would require threads).
As John Zwinck mentioned, io_service::run() blocks - it's a main asio loop that dispatches completion handlers. However, instead of calling run, you can "manually" process the io_service queue by interleaving io_service::poll_one with your loop:
for (long i=0; i<1000000000; i++) {
double x=std::rand();
std::cout << x << std::endl;
std::cout.flush();
vec.push_back(x);
io_service.poll_one();
}