I want to sample values I get from a gpio 4000 times per second, currently I do something like that:
std::vector<int> sample_a_chunk(unsigned int rate, unsigned int block_size_in_seconds) {
std::vector<std::int> data;
constexpr unsigned int times = rate * block_size_in_seconds;
constexpr unsigned int delay = 1000000 / rate; // microseconds
for (int j=0; j<times; j++) {
data.emplace_back(/* read the value from the gpio */);
std::this_thread::sleep_for(std::chrono::microseconds(delay));
}
return data;
}
yet according to the reference sleep_for is guaranteed to wait for at least the specified amount of time.
How can I have my system wait for the exact amount of time, or at least achieve the best possible accuracy?
How can I be sure of the time resolution of my system?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…