项目作者: berndporr

项目描述 :
C++ timer: wrapper around the standard Linux C timer to make your life easier
高级语言: C++
项目地址: git://github.com/berndporr/cppTimer.git
创建时间: 2018-01-29T21:33:16Z
项目社区:https://github.com/berndporr/cppTimer

开源协议:GNU General Public License v3.0

下载


CppTimer

Generic C++ Timer for Linux

It’s a wrapper around the Linux timers. There are two ways of using
the timer: by overloading the timerEvent() method in the CppTimer class
itself (fastest) or by registering a callback class called Runnable
with an overloaded run() method.

Installation

  1. cmake .
  2. make
  3. sudo make install

Usage (overloading the timer event)

The doxygen generated online docs are here: https://berndporr.github.io/cppTimer/

Include CppTimer.h in your program. That’s it.

  1. TARGET_LINK_LIBRARIES(your_project_title cpptimer rt)

Create the Timer class

  1. class MyTimer : public CppTimer {
  2. void timerEvent() {
  3. // your timer event code here
  4. }
  5. };

where you override timerEvent with your function.

Run the Timer class

The timer is programmed in nanoseconds:

  1. MyTimer myTimer;
  2. // every 500000ns
  3. myTimer.startns(500000);

or milliseconds:

  1. // every 200ms
  2. myTimer.startms(200);

As soon as start returns the timer fires instantly and
then at the specified interval.

Demo program

To run demo.cpp just do cmake ., make and then ./demo.

Callback interface version

Instead of overloading the run() method in the timer class you can
overload the run() method in the Runnable class and then register
this class with the timer class. Check out demo_runnable which
demonstrates how to use this method.

Callback via lambda function

Here, the callback is established with the help of a lambda function
instead of a callback interface. This allows direct registering
of a method of the receiving class but one needs to get used to the
lambda function syntax.

Unit tests

Run:

  1. ctest

That’s it. Enjoy!