Code Monkey home page Code Monkey logo

chrono's Introduction

Chrono

C++11 Timer wrappers based on Boost.Asio timers

  • Repeatable
// set by construct argument or as below
timer.Recursive(true);
  • Cancellable
// Stopped by destruction or as below
timer.Stop();
  • Point-based or Duration-based
  • Thread-safed callback supported by asio::strand as io executor
boost::asio::io_context ioc;
boost::asio::io_context::strand strand{ioc};
DurationTimer timer{strand, std::chrono::second{1}};

Uasge

Point timer

  • One-time timer by default

  • Declaration

template <class IntervalType = std::chrono::hours,
          class IOExecutor   = boost::asio::io_context,
          class Timer        = boost::asio::system_timer,
          class TimePoint    = GetTimeType<Timer>>
class PointTimer
  • Example
auto second_from_now =
   std::chrono::system_clock::now() + std::chrono::seconds{1};
boost::asio::io_context ioc;

PointTimer timer{ioc, second_from_now};
timer.Start([]() { std::cout << "timer expired." << std::endl; }); 

ioc.run();

Duration timer

  • Repeat by default

  • Declaration

template <class IntervalType = std::chrono::milliseconds,
          class IOExecutor   = boost::asio::io_context,
          class Timer        = boost::asio::steady_timer>
class DurationTimer
  • Example
auto start_time = std::chrono::steady_clock::now();
boost::asio::io_context ioc;

DurationTimer timer{ioc, std::chrono::duration<double>{1}, false};
timer.Start([]() { std::cout << "timer expired." << std::endl; });

ioc.run();

Point timer with boost ptime

  • Example
using namespace boost::posix_time;
auto second_from_now = second_clock::universal_time() + seconds{1};
boost::asio::io_context ioc;

PointTimer<hours, boost::asio::io_context, boost::asio::deadline_timer> timer{
    ioc, second_from_now};
timer.Start([]() { std::cout << "timer expired." << std::endl; });
ioc.run();

Duration timer with thread-safed callback

  • Example
auto start_time = std::chrono::steady_clock::now();
boost::asio::io_context ioc;
boost::asio::io_context::strand strand{ioc};

DurationTimer timer{strand, std::chrono::second{1}, false};
timer.Start([]() { std::cout << "timer expired." << std::endl; });

ioc.run();

chrono's People

Contributors

qduyang avatar

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.