ssTs is a time-based Task Scheduler, written in modern C++.
Header only, with no external dependencies.
Copy the include folder, that contains the 3 header files task.hpp, task_pool.hpp and task_scheduler.hpp within your project sources or set your include path to it and just build your code.
ssTs requires a C++17 compiler.
It is possible to install the library using:
$ git clone https://github.com/stefanolusardi/task_scheduler.git
$ cd ssts && mkdir build && cd build
$ cmake -G<GENERATOR> -DCMAKE_BUILD_TYPE=<Debug|Release> -DSSTS_INSTALL_LIBRARY=True -DCMAKE_INSTALL_PREFIX=</install/folder/path> ..
$ cmake --build . --config <Debug|Release> --target install
- Windows 10 - Visual Studio 2019
- Windows 10 - Clang 10.0
- Windows 10 - Clang 9.0
- Ubuntu 20.04 - GCC 9.3
- Ubuntu 20.04 - Clang 9.0
- Ubuntu 20.04 - Clang 10.0
- Running a task is as simple as:
// Create a Task Scheduler instance
ts::task_scheduler s(2);
// Start Task Scheduler
s.start()
// Spawn a one-shot task in 5 seconds
s.in(5s, []{std::cout << "Hello!" << std::endl;});
- To retrieve a task result you can do:
// Launch a task with parameters and wait for its return value
std::future f = s.at(std::chrono::steady_clock::now() + 2s,
[](auto x, auto y){
std::cout << "Input parameters: " << x << y << std::endl;
return x+1;
}, 42, "fourty-two");
std::cout << "Task result: " << f.get() << std::endl; // prints 43
- It's possible to start a task giving it a task id to be able to manipulate it later:
// Check if a task is currently scheduled
// (i.e. already inserted in the scheduler)
s.is_scheduled("my_task_id"); // false
// Check if a task is currently enabled
// (i.e. inserted and allowed to execute, by default any task is enabled)
s.is_enabled("my_task_id"); // false
// Start a recursive task with a task id that is scheduled every second
s.every("my_task_id", 1s, [](auto p){std::cout << "Hello! " << p << std::endl;}, "some_task_parameter");
s.is_scheduled("my_task_id"); // true
s.is_enabled("my_task_id"); // true
// Disable "my_task_id"
s.set_enabled("my_task_id", false);
s.is_scheduled("my_task_id"); // true
s.is_enabled("my_task_id"); // false
// Remove "my_task_id"
s.remove_task("my_task_id"s);
s.is_scheduled("my_task_id"); // false
s.is_enabled("my_task_id"); // false
// Stop Task Scheduler (by default it's always properly stopped when it goes out of scope)
s.stop();
- Nested task creation and execution is also supported:
s.every("parent_task_id", 1s,
[&s]{
std::cout << "Parent Task!" << std::endl;
s.in("inner_task_id", 100ms, []{ std::cout << "Child Task!" << std::endl; });
});
Documentation sources are located in the docs folder.
An online version of the latest docs can be found here.
Docs are automatically generated from source code at every commit using Doxygen, Breathe and Sphinx and are hosted on Read The Docs.
It is possible to build the docs from CMake (Doxygen, Breathe and Sphinx are required) using:
$ git clone https://github.com/stefanolusardi/task_scheduler.git
$ cd ssts && mkdir build && cd build
$ cmake -G<GENERATOR> -DSSTS_BUILD_DOCS=True ..
$ cmake --build .
Examples are located within the examples folder.
It is possible to build and install the examples using:
$ git clone https://github.com/stefanolusardi/task_scheduler.git
$ cd ssts && mkdir build && cd build
$ cmake -G<GENERATOR> -DCMAKE_BUILD_TYPE=<Debug|Release> -DSSTS_BUILD_EXAMPLES=True -DSSTS_INSTALL_LIBRARY=True -DSSTS_INSTALL_EXAMPLES=True ..
$ cmake --build . --config <Debug|Release>
Tests are located within the tests folder.
GoogleTest is the only 3rd party library used and it is required only to build the tests target.
It is integrated using DownloadGoogleTest.cmake script.
It is possible to build the tests using:
$ git clone https://github.com/stefanolusardi/task_scheduler.git
$ cd ssts && mkdir build && cd build
$ cmake -G<GENERATOR> -DCMAKE_BUILD_TYPE=<Debug|Release> -DSSTS_BUILD_TESTS=True ..
$ cmake --build . --config <Debug|Release>