Giter VIP home page Giter VIP logo

lambdataskscheduler's Introduction

Alt Text

LambdaTaskScheduler

LambdaTaskScheduler is a lightweight C++ task scheduling library that enables asynchronous execution of one-time and recurring tasks. It offers a straightforward interface for managing tasks and executing them on specified intervals.

Key Features

  • Task Management: Schedule both one-time and recurring tasks effortlessly.
  • Customizable: Tailor tasks with adjustable intervals, durations, and support for external stop conditions.
  • Thread-Safe Execution: Ensures safe concurrent task execution using mutex and condition variables.
  • Dynamic Thread Pool: Flexible task scheduler with a dynamically adjustable thread pool size.
  • Modern C++ Implementation: Clean and efficient codebase leveraging modern C++ features.
  • Broad Compatibility: Designed to work with C++11, C++14, C++17, and C++20 standards, accommodating a wide range of development environments.

Installation

To integrate LambdaTaskScheduler into your project, simply include the necessary header files and link against the library.

#include "TaskScheduler.hpp"

Usage

Creating Tasks

Define tasks using the provided Task interface. Implement one-time tasks and recurring tasks with ease.

// Define a one-time task
auto oneTimeTask = std::make_unique<OneTimeTask>([] {
    // Task logic here
    std::cout << "Executing one-time task" << std::endl;
});

// Define a recurring task with a specified interval and duration
auto recurringTask = std::make_unique<RecurringTask>(1, [] {
    // Task logic here
    std::cout << "Executing recurring task" << std::endl;
}, std::chrono::milliseconds(1000), std::chrono::milliseconds(5000)); // Interval of 1 second, duration of 5 seconds

// Define a recurring task with an external stop condition
std::atomic<bool> stopFlag(false);
auto externalStopConditionTask = std::make_unique<RecurringTask>(2, [] {
    // Task logic here
    std::cout << "Executing task with external stop condition" << std::endl;
}, std::chrono::milliseconds(1000), &stopFlag); // Interval of 1 second, external stop condition

Scheduling Tasks

Add tasks to the task scheduler to initiate their execution.

TaskScheduler scheduler;
scheduler.addTask(1, std::move(oneTimeTask)); // Assign task ID 1 to the one-time task
scheduler.addTask(2, std::move(recurringTask)); // Assign task ID 2 to the recurring task
scheduler.addTask(3, std::move(externalStopConditionTask)); // Assign task ID 3 to the task with external stop condition

Stopping Tasks

Stop specific recurring tasks by their identifiers when they are no longer needed or when an external condition is met.

scheduler.stopTask(2); // Stop the recurring task with task ID 2

// Alternatively, stop a specific recurring task when an external condition is met
// For example, when a stop condition atomic flag is set
stopFlag.store(true);

Stopping the Scheduler

Terminate all running tasks by stopping the task scheduler.

scheduler.stop();

Example

Here's a simple example demonstrating the usage of LambdaTaskScheduler with task identifiers:

#include "TaskScheduler.hpp"

int main() {
    TaskScheduler scheduler;

    // Add tasks to the scheduler with task IDs
    scheduler.addTask(1, std::make_unique<OneTimeTask>([] {
        std::cout << "Executing one-time task" << std::endl;
    }));

    scheduler.addTask(2, std::make_unique<RecurringTask>([] {
        std::cout << "Executing recurring task" << std::endl;
    }, std::chrono::milliseconds(1000), std::chrono::milliseconds(5000))); // Interval of 1 second, duration of 5 seconds

    std::atomic<bool> stopFlag(false);
    scheduler.addTask(3, std::make_unique<RecurringTask>([] {
        std::cout << "Executing task with external stop condition" << std::endl;
    }, std::chrono::milliseconds(1000), &stopFlag)); // Interval of 1 second, external stop condition

    // Wait for tasks to complete
    std::this_thread::sleep_for(std::chrono::seconds(10));

    // Stop the scheduler
    scheduler.stop();

    return 0;
}

Contributing

We welcome contributions from the community!

  • Fork the repository
  • Make your changes
  • Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

lambdataskscheduler's People

Contributors

90th avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.