Implementing a low-overhead interval timer with C++ in Linux -


I am trying to implement a background user-space program that works for every 100ms in Linux, Will do I can do this by using an alarm signal in Linux and the following structure is a way to implement my interval timer.

  zero timer_handler (int sigm) {printf ("in timer handler! \ N"); } Int main () {struct sign s_action; Straight immirivir timer; / * Set timer * / / * set timer_handler as signal handler for SIGVTALRM * / mimet (& amp; s_action, 0, size (s_action)); S_action.sa_handler = & amp; Timer_handler; Signtion (SIGVTRM, and S_Action, Faucet); / * Timer configuration for 100ms * / timer.it_value = v_sec = 0; Timer.it_value.tv_usec = 100000; Timer.it_interval.tv_sec = 0; Timer.it_interval.tv_usec = 100000; / * Set timer * / statimeter (ITIMIvTual, and timer, tap); While (1); }  

However, this approach is not the best way to perform an endless loop (in terms of performance). This enhances CPU usage significantly After the start of the background, the performance of my other programs is reduced to 15% as a mash.

What I ideally want is that there should be a program, as long as the timer does not obstruct, it will remain asleep. Multi threading seems an option but I am not very experienced with that topic. I appreciate any suggestions or indicators that how to implement this program with a minimum overhead.

read ,,,, and.

Your signal handler is incorrect (it should not be called printf , it can be write ).

You can do

  while (1) voting (zero, 0, 1);  

But perhaps a real event loop should be better by using poll on initial file descriptor from timerfd_create .

I certainly believe that you are quite convinced that every periodic work is much less than this period. (Like each work should not exceed 50 milliseconds, but it has a duration of 100 milliseconds).


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -