纳秒级C++程序空闲/节流
Nanosecond-level C++ program idling / throttling
我正在编写一个运行微控制器模拟器的C++程序。运行模拟器的线程在如下所示的循环中执行此操作:
while(should_run) {
simulator->RunSingleClockCycle();
}
在我的笔记本电脑上,它设法以大约 60 MHz 的速度运行模拟器。由于我只需要模拟器以 16MHz 的速度运行,因此我可以在此 while 循环中使用 busyloop 来减慢它的速度,以便它以 16MHz 运行,因为不可能再睡一纳秒。
由于此线程正在运行模拟器并繁忙循环,因此它会导致我的一个内核上的 CPU 使用率为 100%。如果有一种方法可以以某种方式限制线程,使其以16MHz(无繁忙循环(运行微控制器,它将减少模拟器的资源使用。
是否有任何半跨平台(macOS + linux(的方法可以在C++中实现这种线程限制?
在传统硬件上,您永远不会获得纳秒级计时。例如,在我的运行此代码的系统上(不考虑竞争条件(:
#include <thread>
#include <chrono>
#include <future>
#include <iostream>
int main()
{
unsigned int counter = 0;
auto res = std::async(std::launch::async, [&]()
{
while (true)
{
std::cout << "Count : " << counter << 'n';
counter = 0;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
auto timetoWait = 1'000'000'000 / 16'000'000;
while (true)
{
++counter;
std::this_thread::sleep_for(std::chrono::nanoseconds(timetoWait));
}
}
我每秒最多获得 600-700k 的"操作"。远非模拟器应该运行的 1600 万。
你最好只是忙着循环。
你可以使用chrono来实现这个目的,我很确定它适用于Linux和Windows。
我不知道有任何方法可以测量100%准确的时间。 这些时间本身会调用一些时间,但它们相对准确。
#include <iostream>
#include <chrono>
bool wait(long long nanoseconds)
{
// if the number of ns to wait is not zero
if(0 != nanoseconds)
{
// init high resolution clock
std::chrono::high_resolution_clock hrc = {};
// get the start and stop timepoints
std::chrono::time_point<std::chrono::high_resolution_clock> start = hrc.now(),
stop = hrc.now();
// calculate the number of ns passed by subtracting the time at the start from the time at the stop
std::chrono::nanoseconds time_passed = stop - start;
// while the number of ns to wait for is bigger than the number of ns passed
while(nanoseconds > time_passed.count())
{
// get the new stop timepoint
stop = hrc.now();
// calculate the new number of ns passed
time_passed = stop - start;
}
// the wait has ended
return true;
}
// the function failed
return false;
}
int main()
{
printf("startn");
// wait for 5 seconds
wait(5000000000);
printf("stopn");
getchar();
return 0;
}
您也可以使用 rdtsc 指令来提高准确性,但我无法使示例代码正常工作,所以我还是会发布它。
这是汇编代码(我的IDE,Visual Studio,不支持x64上的内联汇编,所以我不得不单独编写(
.model flat, c
.code
get_curr_cycle proc
cpuid
cpuid
cpuid
rdtsc
shl edx, 32
or edx, eax
mov eax, edx
retn
get_curr_cycle endp
end
下面是 C++ 代码。
#include <iostream>
extern "C" unsigned int get_curr_cycle();
bool wait(long long nanoseconds)
{
if(0 != nanoseconds)
{
unsigned int start = get_curr_cycle(),
stop = get_curr_cycle();
unsigned int time_passed = (stop - start);
while(nanoseconds > time_passed)
{
stop = get_curr_cycle();
time_passed = (stop - start);
}
}
return false;
}
int main()
{
printf("startn");
// wait for 5 seconds
wait(5000000000);
printf("stopn");
getchar();
return 0;
}
我认为您可能正在寻找的是类似的东西
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
另请参阅:
https://en.cppreference.com/w/cpp/thread/sleep_for
https://en.cppreference.com/w/cpp/thread/sleep_until
https://en.cppreference.com/w/cpp/chrono/duration
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 纳秒级C++程序空闲/节流
- 将 tiff 图像转换为字符串,以作为二进制应用程序/八位字节流 (C++) 发布
- 在Java程序中通过UDP接收c++字节流