Scheduler - Modern C++ Scheduling Library

Overview

Scheduler

Modern C++ Header-Only Scheduling Library. Tasks run in thread pool. Requires C++11 and ctpl_stl.h in the path.

Inspired by the Rufus-Scheduler gem. Offers mostly the same functionality.

  #include "Scheduler.h"

  // number of tasks that can run simultaneously
  // Note: not the number of tasks that can be added,
  //       but number of tasks that can be run in parallel
  unsigned int max_n_threads = 12;

  // Make a new scheduling object.
  // Note: s cannot be moved or copied
  Bosma::Scheduler s(max_n_threads);

  // every second call message("every second")
  s.every(1s, message, "every second");

  // in one minute
  s.in(1min, []() { std::cout << "in one minute" << std::endl; });

  // in one second run lambda, then wait a second, run lambda, and so on
  // different from every in that multiple instances of the function will not be run concurrently
  s.interval(1s, []() {
    std::this_thread::sleep_for(5s);
    std::cout << "once every 6s" << std::endl;
  });

  s.every(1min, []() { std::cout << "every minute" << std::endl; });

  // https://en.wikipedia.org/wiki/Cron
  s.cron("* * * * *", []() { std::cout << "top of every minute" << std::endl; });

  // Time formats supported:
  // %Y/%m/%d %H:%M:%S, %Y-%m-%d %H:%M:%S, %H:%M:%S
  // With only a time given, it will run tomorrow if that time has already passed.
  // But with a date given, it will run immediately if that time has already passed.
  s.at("2017-04-19 12:31:15", []() { std::cout << "at a specific time." << std::endl; });

  s.cron("5 0 * * *", []() { std::cout << "every day 5 minutes after midnight" << std::endl; });

See example.cpp for a full example.

Comments
  • Unsupported cron string leads to unhandled exception

    Unsupported cron string leads to unhandled exception

    cronScheduler.cron("*/5 * * * *", []() { Foo(); });

    This leads to an "invalid stoi argument" exception, at least on MSVC 15.6. I think it's the responsability of the library to gracefully handle incorrect or unsupported arguments for its functions.

    opened by Tickwick 5
  • call bosma schedulter function from inside function!!

    call bosma schedulter function from inside function!!

    i want to call function from inside function in class, but dont working! working only on main ! note:scheduling object is inside from member functio of class

    opened by bnadem 4
  • Compiling

    Compiling

    I cannot get this to compile with either clang or g++, e.g.

    Projects/Scheduler/Cron.h:13:41: error: no viable conversion from 'time_point<[...], duration<[...], ratio<[...], __static_lcm<ratio<1, 1000000>::den, ratio<1, 1000000000>::den>::value aka 1000000000>>>' to 'const time_point<[...], duration<[...], ratio<[...], 1000000>>>' auto tm_adjusted = Clock::to_time_t(tp + time);

    Also, do you have any plans to use make this work with C++11?

    opened by neilcook 3
  • at method run at the begin of execution.

    at method run at the begin of execution.

    Hi,

    Is it normal behavior that the function called like for instance: s.at("2018-02-27 15:39:30", { std::cout << "at a specific time. RELAY ON" << std::endl; });

    will print at a specific time. RELAY ON not only at 2018-02-27 15:39:30 but as well just after program start execution. I my case it seems to be just like that, but I expected to print only on the specified time. Do you have any hint how to make it working?

    I can ensure that my system time is before of the set time in the method call.

    BR

    Krzysztof

    opened by klukaspl 2
  • 'now' method

    'now' method

    Hi! Can you add 'now' method for executing some code inside Bosma::Scheduller workers thread pool like this: template<typename _Callable, typename... _Args> void now(_Callable &&f, _Args &&... args) { in(std::chrono::nanoseconds(0), std::forward<_Callable>(f), std::forward<_Args>(args)...); } This feature in my project help me for free main thread execution using Bosma::Scheduller features. To your notice =)

    opened by asmyasnikov 2
  • Crash on manage_task while program end and Scheduler destructor called

    Crash on manage_task while program end and Scheduler destructor called

    I just tried your example.cpp and modified the sleep time from 10 minutes to 10 seconds in main thread. That made my program crash on manage_task method while program ended.

    I use std::cerr to show the execute order, then I found that destructor called and manage_task called after destructor. That made program crashed.

    I'm using VS2017 to build program. Please check this problem. Thanks.

    opened by cloud9370 2
  • Passing arguments to schedules

    Passing arguments to schedules

    Hello,

    Is there a way to pass arguments to the schedules.

    I want to do something similar to the following: for (int i = 0; i < 10; i++) s.cron("* * * * *", { std::cout << "top of every minute "<< i << std::endl; });

    however this will have all the schedules print 9.

    opened by Mitchell-Hampton 1
  • Cron functionality observes UTC instead of local time

    Cron functionality observes UTC instead of local time

    When setting a specific time for a cron job, the time has to be in UTC in order for the task to run at the correct time point.

    It would be ideal if the time given could be read as local time.

    opened by ChemiCalChems 1
  • Method 'now' and bug fix

    Method 'now' and bug fix

    I was read https://github.com/Bosma/Scheduler/issues/2 too late (after my pool request). Commit https://github.com/Bosma/Scheduler/pull/6/commits/6a91a81cf7806e372837165f0f0c564f8ef9e9e2 excess, but it do not bad things =) My commit https://github.com/Bosma/Scheduler/pull/6/commits/f0d35044586799f52473fa3747b0ceb358a8c060 to your notice

    opened by asmyasnikov 0
  • Callback not triggering from the DLL

    Callback not triggering from the DLL

    The callback is working in the main function but when I am using Scheduler from DLL the function from the DLL is called perfectly but the callback in the same function is not triggering!

    opened by leo9223 0
  • Time Drift using every

    Time Drift using every

    I am using an every call to run a function once a minute. this is to happen exactly on the minute and zero second. It starts out working perfectly, however over time, the execution starts to drift, and executes a second, then 2, then 3... later. Is there something that can be done to improve the precision of execution ?

    opened by johnhlaferriere 1
  • error when using Bosma::Scheduler in the class

    error when using Bosma::Scheduler in the class

    I try to using Bosma::Scheduler in my application, I have a TestScheduler define as follow: ` class TestScheduler : public QWidget { Q_OBJECT

    public: TestScheduler(QWidget* parent = 0); ~TestScheduler(); void print(); } here is it implementation, very simple: TestScheduler::TestScheduler(QWidget* parent) : QWidget(parent) { Bosma::Scheduler s(4); s.at("2021-05-26 15:50:00", &TestScheduler::print, this); }

    void TestScheduler::print() { qDebug() << "ok"; } ` I run this above but has an error, scheduler do not working. I try to investigate this and found that an error has occupied in the file "ctpl_stl.h", I try print some simple string, the order of thread pool do as follow:

    • init threads
    • resize
    • push function after that thread pool do destroy selt, so that scheduler do not working. Please solve this problem because now I only use Bosma::Scheduler in the main function, thank in advanced!!!
    opened by phamtung1234 0
  • Error when compiling in Visual Studio Community 2019 16.8.4

    Error when compiling in Visual Studio Community 2019 16.8.4

    Hello!!

    When compiling in Visual Studio Community 2019 16.8.4, with toolset v142, it throws the next error:

    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xmemory(230,12): error C2664: 'Bosma::EveryTask::EveryTask(std::chrono::system_clock::duration,std::function<void (void)> &&,bool)': cannot convert argument 2 from '_Ty' to 'std::function<void (void)> &&'
    1>        with
    1>        [
    1>            _Ty=std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>
    1>        ]
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xmemory(228,1): message : Reason: cannot convert from '_Ty' to 'std::function<void (void)>'
    1>        with
    1>        [
    1>            _Ty=std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>
    1>        ]
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xmemory(230,33): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>E:\Devs\NetworkTonic\Service\NTSupervisor\External libraries\scheduler\Scheduler.h(37,9): message : see declaration of 'Bosma::EveryTask::EveryTask'
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\memory(1563): message : see reference to function template instantiation 'void std::_Construct_in_place<_Ty,const std::chrono::system_clock::duration&,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>>(_Ty &,const std::chrono::system_clock::duration &,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &> &&) noexcept(false)' being compiled
    1>        with
    1>        [
    1>            _Ty=Bosma::EveryTask
    1>        ]
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\memory(2207): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<const std::chrono::system_clock::duration&,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>>(const std::chrono::system_clock::duration &,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &> &&)' being compiled
    1>        with
    1>        [
    1>            _Ty=Bosma::EveryTask
    1>        ]
    1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\memory(2208): message : see reference to function template instantiation 'std::_Ref_count_obj2<_Ty>::_Ref_count_obj2<const std::chrono::system_clock::duration&,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>>(const std::chrono::system_clock::duration &,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &> &&)' being compiled
    1>        with
    1>        [
    1>            _Ty=Bosma::EveryTask
    1>        ]
    1>E:\Devs\NetworkTonic\Service\NTSupervisor\External libraries\scheduler\Scheduler.h(132): message : see reference to function template instantiation 'std::shared_ptr<Bosma::EveryTask> std::make_shared<Bosma::EveryTask,const std::chrono::system_clock::duration&,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &>>(const std::chrono::system_clock::duration &,std::_Binder<std::_Unforced,void (__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t &,sensorData_t &> &&)' being compiled
    1>E:\Devs\NetworkTonic\Service\NTSupervisor\Sources\serviceController.cpp(107): message : see reference to function template instantiation 'void Bosma::Scheduler::every<void(__cdecl &)(const templateData_t &,const sensorData_t &,bool),templateData_t&,sensorData_t&>(const std::chrono::system_clock::duration,_Callable,templateData_t &,sensorData_t &)' being compiled
    1>        with
    1>        [
    1>            _Callable=void (__cdecl &)(const templateData_t &,const sensorData_t &,bool)
    1>        ]
    
    

    As far I can understand, there is a problem with std::move. It was working flawlessly with previous version of VS :(

    Thank you :)

    opened by jesusdelrio 0
  • Scheduler::every is not steady to time changes

    Scheduler::every is not steady to time changes

    After change system time to the some moment in the past, Scheduler::every will not work correctly because Clock::now value will change (https://github.com/Bosma/Scheduler/blob/master/Scheduler.h#L182) because Clock is a std::chrono::system_clock.

    opened by Vertaler 0
Owner
Spencer Bosma
Spencer Bosma
A easy to use multithreading thread pool library for C. It is a handy stream like job scheduler with an automatic garbage collector. This is a multithreaded job scheduler for non I/O bound computation.

A easy to use multithreading thread pool library for C. It is a handy stream-like job scheduler with an automatic garbage collector for non I/O bound computation.

Hyoung Min Suh 12 Jun 4, 2022
OOX: Out-of-Order Executor library. Yet another approach to efficient and scalable tasking API and task scheduling.

OOX Out-of-Order Executor library. Yet another approach to efficient and scalable tasking API and task scheduling. Try it Requirements: Install cmake,

Intel Corporation 18 Oct 25, 2022
A task scheduling framework designed for the needs of game developers.

Intel Games Task Scheduler (GTS) To the documentation. Introduction GTS is a C++ task scheduling framework for multi-processor platforms. It is design

null 422 Nov 26, 2022
Operating system project - implementing scheduling algorithms and some system calls for XV6 OS

About XV6 xv6 is a modern reimplementation of Sixth Edition Unix in ANSI C for multiprocessor x86 and RISC-V systems. It was created for pedagogical p

Amirhossein Rajabpour 22 Oct 24, 2022
Bistro: A fast, flexible toolkit for scheduling and running distributed tasks

Bistro is a flexible distributed scheduler, a high-performance framework supporting multiple paradigms while retaining ease of configuration, management, and monitoring.

Facebook 1.1k Nov 19, 2022
Px - Single header C++ Libraries for Thread Scheduling, Rendering, and so on...

px 'PpluX' Single header C++(11/14) Libraries Name Code Description px_sched px_sched.h Task oriented scheduler. See more px_render px_render.h Multit

PpluX 450 Nov 25, 2022
A hybrid thread / fiber task scheduler written in C++ 11

Marl Marl is a hybrid thread / fiber task scheduler written in C++ 11. About Marl is a C++ 11 library that provides a fluent interface for running tas

Google 1.4k Nov 29, 2022
afl/afl++ with a hierarchical seed scheduler

This is developed based on AFLplusplus (2.68c, Qemu mode), thanks to its amazing maintainers and community Build and Run Please follow the instruction

null 47 Nov 25, 2022
Forkpool - A bleeding-edge, lock-free, wait-free, continuation-stealing scheduler for C++20

riften::Forkpool A bleeding-edge, lock-free, wait-free, continuation-stealing scheduler for C++20. This project uses C++20's coroutines to implement c

Conor Williams 128 Nov 15, 2022
Bikeshed - Lock free hierarchical work scheduler

Branch OSX / Linux / Windows master master bikeshed Lock free hierarchical work scheduler Builds with MSVC, Clang and GCC, header only, C99 compliant,

Dan Engelbrecht 81 Oct 26, 2022
EnkiTS - A permissively licensed C and C++ Task Scheduler for creating parallel programs. Requires C++11 support.

Support development of enkiTS through Github Sponsors or Patreon enkiTS Master branch Dev branch enki Task Scheduler A permissively licensed C and C++

Doug Binks 1.4k Nov 29, 2022
Cpp-taskflow - Modern C++ Parallel Task Programming Library

Cpp-Taskflow A fast C++ header-only library to help you quickly write parallel programs with complex task dependencies Why Cpp-Taskflow? Cpp-Taskflow

null 4 Mar 30, 2021
CTPL - Modern and efficient C++ Thread Pool Library

CTPL Modern and efficient C++ Thread Pool Library A thread pool is a programming pattern for parallel execution of jobs, http://en.wikipedia.org/wiki/

null 1.1k Nov 24, 2022
Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all

concurrencpp, the C++ concurrency library concurrencpp is a tasking library for C++ allowing developers to write highly concurrent applications easily

David Haim 1.2k Dec 2, 2022
A modern thread pool implementation based on C++20

thread-pool A simple, functional thread pool implementation using pure C++20. Features Built entirely with C++20 Enqueue tasks with or without trackin

Paul T 146 Nov 26, 2022
Bolt is a C++ template library optimized for GPUs. Bolt provides high-performance library implementations for common algorithms such as scan, reduce, transform, and sort.

Bolt is a C++ template library optimized for heterogeneous computing. Bolt is designed to provide high-performance library implementations for common

null 359 Nov 27, 2022
oneAPI DPC++ Library (oneDPL) https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/dpc-library.html

oneAPI DPC++ Library (oneDPL) The oneAPI DPC++ Library (oneDPL) aims to work with the oneAPI DPC++ Compiler to provide high-productivity APIs to devel

oneAPI-SRC 644 Dec 3, 2022
ArrayFire: a general purpose GPU library.

ArrayFire is a general-purpose library that simplifies the process of developing software that targets parallel and massively-parallel architectures i

ArrayFire 4k Nov 28, 2022
A C++ GPU Computing Library for OpenCL

Boost.Compute Boost.Compute is a GPU/parallel-computing library for C++ based on OpenCL. The core library is a thin C++ wrapper over the OpenCL API an

Boost.org 1.3k Nov 26, 2022