I'm writing scheduler for my system, that should collect data from sensors.
My scheduler have got list with planed tasks. Scheduler running in one thread and running tasks in individual threads.
Please, recommend me timer in C++, that support soft real time.
I'm writing code for vanilla Linux.
P.S. I didn't found same question on StackOverflow.
P.S.S Sorry for my bad English
As you clarified about that soft real time requirement in your comment:
"But i want timer guaranty sleeping time with ms resolution."
From standard c++ you can check the actually available resolution for e.g. the std::chrono::high_resolution_clock
or std::chrono::system_clock
using the std::chrono::high_resolution_clock::period
member type. If your current system implementation doesn't meet the required resolution, you may throw an exception or such.
Here's a demo how to do it:
#include <chrono>
#include <ratio>
#include <stdexcept>
#include <iostream>
int main() {
try {
// Uncomment any of the following checks for a particular
// resolution in question
if(std::ratio_less_equal<std::nano
,std::chrono::system_clock::period>::value) {
// if(std::ratio_less_equal<std::micro
// ,std::chrono::system_clock::period>::value) {
// if(std::ratio_less_equal<std::milli
// ,std::chrono::system_clock::period>::value) {
// if(std::ratio_less_equal<std::centi
// ,std::chrono::system_clock::period>::value) {
throw std::runtime_error
("Clock doesn't meet the actual resolution requirements.");
}
}
catch(const std::exception& ex) {
std::cout << "Exception: '" << ex.what() << "'" << std::endl;
}
std::cout << "The curently available resolution is: "
<< std::chrono::system_clock::period::num << "/"
<< std::chrono::system_clock::period::den
<< " seconds" << std::endl;
}
The output (at ideone system) is:
Exception: 'Clock doesn't meet the actual resolution requirements.'
The curently available resolution is: 1/1000000000 seconds
For sleeping a predefined time period, you can use std::thread::sleep_for()
to realize a timer.