我试图想出一个替代 sleep() 的跨平台方法,但我的代码不太有效

发布于 2025-01-01 17:21:41 字数 591 浏览 2 评论 0原文

我正在做一个基于 snap 游戏的初级 C++ 程序。

当我将纸牌对象输出到控制台时,由于计算机的处理速度,自然会出现已发牌的完整列表。我认为如果我可以在每张牌发牌之间暂停一下,这样人们就可以真正观察到每张牌的发牌情况,这可能会很好。因为我一直在 Linux 和 Windows 上工作并且已经有了 <时间>包括我想出了这个小解决方案:

for(;;){
            if( (difftime(time(0),lastDealTime)) > 0.5f){ //half second passed
                cout << currentCard <<endl;
                lastDealTime = time(0);
                break;
            }
        }

起初我以为它有效,但后来当我尝试加快发牌过程时,我意识到将控制值更改为0.5(我的目标是每半秒发一张牌)似乎没有任何效果..我尝试将其更改为每 0.05 秒处理一次,但没有什么区别,我猜卡片似乎仍然每秒输出一次。

关于为什么这行不通的任何观察?谢谢!

I'm doing a little beginner c++ program based on the game of snap.

When i output the card objects to the console, because of the computers processing speed naturally, a whole list of the cards that were dealt just appears. I thought it might be nice if i could put a pause between each card deal so that a human could actually observe each card being dealt. Since i'm always working on both Linux and Windows and already had < ctime > included i came up with this little solution:

for(;;){
            if( (difftime(time(0),lastDealTime)) > 0.5f){ //half second passed
                cout << currentCard <<endl;
                lastDealTime = time(0);
                break;
            }
        }

At first i thought it had worked but then when i tried to speed up the dealing process later i realised that changing the control value of 0.5 (i was aiming for a card deal every half a second) didn't seem to have any effect.. i tried changing it to deal every 0.05 seconds and it made no difference, cards still seemed to be output every second i would guess.

Any observations as to why this wouldn't be working? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宣告ˉ结束 2025-01-08 17:21:41

time() 的分辨率是一秒——即返回值是整数秒。你永远不会看到小于一秒的差异。

usleep() 位于标准 C 库中——它的分辨率以微秒为单位,因此请使用它。

The resolution of time() is one second -- i.e., the return value is an integral number of seconds. You'll never see a difference less than a second.

usleep() is in the standard C library -- it has a resolution in microseconds, so use that instead.

挖个坑埋了你 2025-01-08 17:21:41

time()difftime() 的分辨率为秒,因此没有
使用它们来管理小于一秒的间隔的方法;甚至为了
一秒的间隔,它们不可用,因为抖动可能高达
还有一秒钟。

在这种情况下,解决方案是定义某种计时器类,其中包含
头文件中的系统独立接口,但系统相关
源文件;根据系统,您可以编译一个源文件或
另一个。 Windows 和 Linux 都有管理时间的方法
更高的分辨率。

time() and difftime() have a resolution of a second, to there's no
way to use them to manage intervals of less than a second; even for
intervals of a second, they're not usable, since the jitter may be up to
a second as well.

In this case, the solution is to define some sort of timer class, with a
system independent interface in the header file, but system dependent
source files; depending on the system, you compile one source file or
the other. Both Windows and Linux do have ways of managing time with
higher resolution.

楠木可依 2025-01-08 17:21:41

如果您想确保卡片按照您请求的时间间隔精确发牌,那么您可能也应该创建一个计时器类。我们使用:

在 Windows 中使用 QueryPerformanceFrequency 获取系统滴答时间和 QueryPerformanceCounter 获取刻度

在 Mac Carbon 上,使用 DurationToAbsolute 获取系统刻度时间,使用 UpTime 获取刻度。

在 Linux 上使用 clock_gettime

对于睡眠使用:

一个 Windows 使用 Sleep();

在 Mac Carbon 上使用 MPDelayUntil();

在 Linux 上使用 nanosleep();

If you want to make sure that the cards deal at precisely the interval you request, then you should probably create a timer class too. We use:

In Windows use QueryPerformanceFrequency to get the system tick time and QueryPerformanceCounter to get the ticks

On Mac Carbon use DurationToAbsolute to get system tick time and UpTime to get the ticks.

On Linux use clock_gettime.

For sleep use:

One Windows use Sleep();

On Mac Carbon use MPDelayUntil();

On Linux use nanosleep();

挽心 2025-01-08 17:21:41

从我看来,你的代码的大问题不是你没有找到睡眠的单平台版本,而是睡眠实际上是为了让CPU在一段时间内停止处理,但是你的不会停止处理,您的应用程序将消耗大量资源。

当然,如果您的计算机专门运行一个应用程序可能并不重要,但现在我们希望我们的计算机不仅仅做一件事。

the big issue with your code from the way I see it is not the fact that you have not found a single-platform version of sleep but the fact that sleep is actually meant to stop the CPU from processing for a period of time, but yours will not stop processing and your application will use up lots of resources.

Of course if your computer is dedicated to just running one application it might not matter, but nowadays we expect our computers to be doing more than just one thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文