GithubHelp home page GithubHelp logo

cpptime's Introduction

C++ Timeouts

A portable, header-only C++11 timer component.

It manages a set of timeouts that, when expired, invoke a callback. It uses features of C++11 in order to avoid platform specific code.

It supports one-shot and periodic timeouts.

Documentation

Please see the documentation in cpptime.h for more detailed information about the implementation.

Implementation Status

We use this timer implementation in some of our products without issues. Judging from the GitHub stars and forks, it seems to be used in other projects as well. Since it was implemented in 2015 and has not seen many issue reports, we assume it is quite stable.

But note that this is not a guarantee and if you find any issues, please report them.

Use Cases and Limitations

Naturally the implementation makes some trade-offs. This makes it useful for some cases, and less so for others.

  • The timer runs completely in user space. This makes it slightly less efficient than other solutions, such as timer_create() or timerfd_create(). However, in many cases, this overhead is acceptable.

  • Given a C++11 capable compiler, the code is portable.

  • The API to add or remove a timeout is arguably nicer than the platform specific alternatives. E.g.

timer.add(seconds(2), [](CppTime::timer_id) { ... });
  • The implementation is small and easy to understand. You can change or extend it to make it better suitable for your use-cases.

Examples

A one shot timer.

using namespace std::chrono;
CppTime::Timer t;
t.add(seconds(2), [](CppTime::timer_id) { std::cout << "yes\n"; });
std::this_thread::sleep_for(seconds(3));

A periodic timer that is first executed after 2 seconds, and after this every second. The timeout event is then removed after 10 seconds. When a timeout event is removed, its attached handler is also freed to clean-up any attached resources.

using namespace std::chrono;
CppTime::Timer t;
auto id = t.add(seconds(2), [](CppTime::timer_id) { std::cout << "yes\n"; }, seconds(1));
std::this_thread::sleep_for(seconds(10));
t.remove(id);

See the tests for more examples.

Usage

To use the timer component, Simply copy cpptime.h into you project. Everything is contained in this single header file.

Tests can be compiled and executed with the following commands, assuming you are on a POSIX machine.

g++ -std=c++11 -Wall -Wextra -o test tests/timer_test.cpp -l pthread
./test

Possible Features

While the current implementation serves us well, there are some features that might potentially be interesting for other use cases. Contact us in case you are interested.

  • Ability to have multiple timer components running.
  • Distribute it as a header only library.
  • Optionally avoid locking.
  • API to use client thread instead of creating its own.
  • API to use client mutex instead of its own.

Known issues

GCC up to version 10 (e.g. used in Ubuntu 20.04 LTS) has an issue where conditional_variable doesn't use the monotonic clock. This leads to unreliable programs when the system clock is moved backwards. See #5 for more details. The fix is to update to a GCC version with the fix applied, e.g. version 10 or greater.

Contributions

Contributions, suggestions, and feature requests are welcome. Please use the GitHub issue tracker.

cpptime's People

Contributors

aleksoid1978 avatar eglimi avatar ksdhans avatar mbehr1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpptime's Issues

bug for remove(0)?

I think there is a bug:
remove(0) (if no timer is active/ever used) will pass the

if(events.size()< id) {
return false;
}
check (events.size()==0 not < 0)
but in the end there is no item. So the check should be extended to e.g.
if (events.size()>0 && events.size()<id)
or id being started with 1 for the first item?

mutithread erro coredump

qestion1 :

thread1 add a period timer and save time_id.
thread2 is the timer thread.

afer thread2 unlock .
thread 1 use the time_id to call Timer::remove() ,
in Timer::remove , delete the time_id and set handler to nullptr,
but thread 1 is executing the handler.
the coredump happen.

lock.unlock();   
events[te.ref].handler(te.ref);
lock.lock();

what should i do to avoid the problem?

question 2:
thread1 add a period timer and save time_id.
thread2 is the timer thread.

thread 1 will call Timer::remove() in handler callback.
while the handler is executing ,it call Timer::remove, But in Timer::remove set handler to nullptr .
and so the program core dump.

	events[id].valid = false;
	events[id].handler = nullptr;

Same lock done for waiting + inserting of time events

We are encountering a serious issue on MacOS where the timeouts are not occurring after some time. The function ´run()´ is waiting for work and inserting time events during the same lock (!). This should be refactored.

Input arguments to handler

Is it possible to extend the timer functionality to pass input argument((void *)arg) to handler function?

Cannot work reliably on Linux

Hi,
I just tested this great timer on both Windows and Ubuntu.
When I set the system time both backward and forward, the timer always invokes the callback function.
But on Ubuntu it only works when I set the system time forward.
I have no idea what's wrong with this code, or the Linux system. Thanks!

Leo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.