延迟执行1秒 [英] Delay execution 1 second

查看:171
本文介绍了延迟执行1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图编写一个简单的基于tick的游戏。我在linux机器上用C ++写。

So I am trying to program a simple tick-based game. I write in C++ on a linux machine. The code below illustrates what I'm trying to accomplish.

for (unsigned int i = 0; i < 40; ++i)
{
    functioncall();
    sleep(1000); // wait 1 second for the next function call
}

工作。它似乎是睡眠了40秒,然后打印输出的结果是从函数调用。

Well, this doesn't work. It seems that it sleeps for 40 seconds, then prints out whatever the result is from the function call.

我也尝试创建一个新的函数叫延迟,它看起来像这个:

I also tried creating a new function called delay, and it looked like this:

void delay(int seconds)
{
    time_t start, current;

    time(&start);

    do
    {
        time(&current);
    }
    while ((current - start) < seconds);
}

任何人?

推荐答案

重申其他人已经说明的具体示例:

To reiterate on what has already been stated by others with a concrete example:

假设你使用 std :: cout 输出,你应该调用 std :: cout.flush(); 正好在sleep命令之前。请参见此MS知识库文章

Assuming you're using std::cout for output, you should call std::cout.flush(); right before the sleep command. See this MS knowledgebase article.

这篇关于延迟执行1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆