Skip to content

Async setInterval & setTimeout implementation using Boost

License

Notifications You must be signed in to change notification settings

YukiWorkshop/cpp-async-timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpp-async-timer

pipeline status Codacy Badge coverage report

C++ async setInterval & setTimeout implementation using Boost.

Requirements

Reasonably new versions of:

  • C++17 compatible compiler
  • CMake
  • Boost

Quick command for Ubuntu users:

apt install build-essential cmake libboost-all-dev

Install

Use of Git submodule and CMake subdirectory is recommended.

mkdir cpp_modules && cd cpp_modules
git submodule add https://github.com/YukiWorkshop/cpp-async-timer

This is a header-only library.

include_directories(cpp_modules/cpp-async-timer)
target_link_libraries(your_project boost_system pthread)

Usage

#include <cpp-async-timer.hpp>

using namespace YukiWorkshop::AsyncTimer;

Print "aaaa" 5 times with an interval of one second:

int cnt = 0;

TimerContext *t = setInterval(iosvc, [&](){
    puts("aaaa");
    cnt++;
    if (cnt == 5) clearInterval(t);
}, 1000);

Print "cccc" after 1.3 seconds:

setTimeout(iosvc, [&](){
    puts("cccc");
}, 1300);

You won't see "dddd" after 1.4 seconds:

TimerContext *t4 = setTimeout(iosvc, [&](){
    puts("dddd"); // You won't see this
}, 1400);

clearTimeout(t4);

Note: The timers will only start when io_service is running.

License

MIT

About

Async setInterval & setTimeout implementation using Boost

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published