c++ - boost deadline_timer minimal example: should I substitute "sleep"? -


i have thread need every 10 ms. have simple code, that:

while (work) {     //     sleep(10000); // boost sleep can used } 

i heard sleep not recommended in general , if substitute deadline_timer overall application performance better, in particular avoid expensive "context switch".

should change sleep deadline_timer , if can give example?

it depends on requirements 10ms.


10ms delay between iterations

if application needs 10ms delay between iterations, sleep fine. assuming work() takes 7 milliseconds complete, timeline result in following:

 time  | action -------+------------ 0.000s | begin work 0.007s | finish work, block 0.017s | finish blocking, begin work 0.024s | finish work, block 0.034s | finish blocking, begin work

it may worth considering using boost.thread's this_thread::sleep_for() readability:

#include <boost/thread.hpp>  int main() {   (;;)   {     work();     boost::this_thread::sleep_for(boost::chrono::milliseconds(10));   } } 

10ms max delay between iterations

if max delay between iterations 10ms, time spent executing work needs reduced 10ms delay. assuming work() takes 7 milliseconds complete, timeline result in following:

 time  | action -------+------------ 0.000s | begin work 0.007s | finish work, block 0.010s | finish blocking, begin work 0.017s | finish work, block 0.020s | finish blocking, begin work

the using timer synchronously tutorial can place start. 1 point consider boost.asio provides few timers. if 10ms delays should not affected changes system clock, consider using steady_timer. otherwise, deadline_timer should fine.

#include <boost/asio/steady_timer.hpp>  boost::asio::io_service io_service; boost::asio::steady_timer timer(io_service);  int main() {   (;;)   {     timer.expires_from_now(boost::chrono::milliseconds(10));     work();     timer.wait();   } } 

another consideration if work() takes 13 milliseconds complete, there no delay between work, max delay has been exceeded. however, results in work() being done every 13 milliseconds, rather work() being done every 10 milliseconds.

 time  | action -------+------------ 0.000s | begin work 0.013s | finish work, block 0.013s | finish blocking, begin work 0.026s | finish work, block 0.039s | finish blocking, begin work

perform work every 10ms

if time takes complete work() exceeds delay, work() not done every 10ms. accomplish this, multiple threads may need used. following timeline 2 threads asynchronously performing work scheduled every 10 milliseconds, takes 13 milliseconds complete:

 time  | thread                   | thread b -------+----------------------------+--------------------------- 0.000s | schedule work, begin work  | 0.010s |                            | schedule work, begin work  0.013s | finish work, block         | 0.020s | schedule work, begin work  | 0.023s |                            | finish work, block 0.030s |                            | schedule work, begin work 0.033s | finish work, block         |

the using timer asynchronously may provide basic introduction. overall idea add work io_service, , every 10 milliseconds thread running io_service selected invoke work(). thread pool size can increased or decreased based on amount of time work() takes complete. in case work takes 7 milliseconds, single thread asynchronously wait on timer.

#include <boost/asio/steady_timer.hpp>  boost::asio::io_service io_service; boost::asio::steady_timer timer(io_service);  void handle_timer(const boost::system::error_code& error);  void schedule_work() {   // schedule more work.   timer.expires_from_now(boost::chrono::milliseconds(10));   timer.async_wait(&handle_timer); }  void handle_timer(const boost::system::error_code& error) {   if (error) return;   schedule_work();   work(); }  int main() {   // add work io_service.   schedule_work();    // create 2 threads run io_service.   boost::thread_group threads;   (std::size_t = 0; < 2; ++i)     threads.create_thread(boost::bind(       &boost::asio::io_service::run, &io_service));    // wait threads finish.   threads.join_all(); } 

when introducing concurrency meet deadline, verify work() thread-safe.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -