1

For a bit of context, I am writing a simple CPU emulator. The emulator process boils down to calling a 'step' function to read and execute the next operation in the program. Currently this is just done as fast as possible in a while loop.

I would like the code to be cross-platform but (unfortunately) windows is the primary target.

I need to be able to execute my Emulator->step() function at regular intervals in the range of 1,000Hz to 100,000Hz.

  • For a slower loop I would simply use sleep() but (on windows at least) it doesn't have the resolution for such a high frequency.

  • I have also toyed with spinning a loop checking a Boost microsecond timer. Ignoring the inaccuracy of this method, it uses up real CPU time whilst it is meant to be 'idle'. I am running several emulated CPUs concurrently in threads so the while loop causes a noticeable impact on performance.

Surely there is a method of doing what I want to do with C++?

4

3 回答 3

0

您不能在 Windows 下精确睡眠(也许 Windows 性能计数器功能有帮助,请参阅Is there a Windows equivalent of nanosleep?)。

您说您在线程中同时运行许多模拟 CPU,因此一种可能的解决方案是丢弃线程并自己为不同的 CPU 进行调度(循环)。

于 2013-04-04T10:27:02.733 回答
0

您不需要任何特殊的睡眠解决方案。在每个循环结束时,只需计算您是否需要睡眠。如果没有,运行下一个循环。如果是这样,请按计算的数量睡觉。如果您在一个循环中多睡一点也没关系,因为这种逻辑会让您在下一个循环中睡得更少。

于 2013-04-04T10:30:35.723 回答
0

如果您使用的是 C++11 编译器,请查看<chrono>. 在那里你可以找到高精度计时器。但请注意,在 Windows 环境中,这些仍然具有较低的准确性,微软有望在下一个版本中修复。

于 2013-04-04T11:21:13.393 回答