Boost Asio deadline_timer on an Android device does not start expiring, but at a later point in time

We are trying to use boost asi deadline_timers on an Android device. we have a requirement when we need to start the timer every 100 ms. I used the code as follows. However, the timer seems to be triggered every 1 second. What should we configure in the Android system in order to set the maximum asio timeout to work as expected?

Note: the following code works as expected on a simple Linux system. What makes it work differently in the Andorid system?

Code

void print(asio::deadline_timer* ptimer, const asio::error_code& err) { struct timeval tval; if(0 == gettimeofday(&tval, NULL)) { std::cout <<" Timer... sec::microsec = "<<tval.tv_sec<<"::"<<tval.tv_usec<< std::endl; } else { std::cout <<" Timer... gettimeofday Error!" << std::endl; } ptimer->expires_from_now(boost::posix_time::milliseconds(100)); ptimer->async_wait(boost::bind(&print, ptimer, asio::placeholders::error)); } 

Android device output

 Timer... sec::microsec = 1298328679::39207 Timer... sec::microsec = 1298328680::46773 Timer... sec::microsec = 1298328681::54624 Timer... sec::microsec = 1298328682::63861 Timer... sec::microsec = 1298328683::65740 Timer... sec::microsec = 1298328684::69301 Timer... sec::microsec = 1298328685::76500 Timer... sec::microsec = 1298328686::85768 
+4
source share
1 answer

You might want to make sure that your Android environment ends up having BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK .

If you look at the default timer_traits in boost/asio/time_traits.hpp , you will see that if this is not defined, asio will use second_clock as its timer.

This parameter is determined from boost/date_time/compiler_config.hpp , provided BOOST_HAS_GETTIMEOFDAY or BOOST_HAS_FTIME . Given your example, you should probably define the first.

I do not know if android is considered its own platform, or if boost defines it as linux. In boost/config/platform/linux.hpp it is defined as:

  //
 // If glibc is past version 2 then we definitely have
 // gettimeofday, earlier versions may or may not have it:
 //
 #if defined (__ GLIBC__) && (__GLIBC__> = 2)
 # define BOOST_HAS_GETTIMEOFDAY
 #endif

You probably want to add an additional condition for Android.

+1
source

Source: https://habr.com/ru/post/1336729/


All Articles