C++ 高效微秒级延时函数实现
在C++中,可以使用chrono库来实现精确到微秒的延时函数,并且占用很小的CPU。具体实现如下:
#include <chrono>
#include <thread>
void delay_us(unsigned int us) {
std::chrono::microseconds duration(us);
std::this_thread::sleep_for(duration);
}
在上面的代码中,使用了std::chrono::microseconds定义了微秒的时间单位,然后使用std::this_thread::sleep_for函数来进行延时操作。调用delay_us函数时,传入需要延时的微秒数即可。
原文地址: https://www.cveoy.top/t/topic/njA8 著作权归作者所有。请勿转载和采集!