【问题标题】:SLEEP: (Sleep or usleep) is not suspending everything in my thread in Linux but it does in Windows? why?SLEEP:(Sleep 或 usleep)在 Linux 中没有暂停我的线程中的所有内容,但在 Windows 中会暂停?为什么?
【发布时间】:2019-07-01 21:50:41
【问题描述】:

在 Windows 中:

    for (int i = 0; i < 100; i++)
    {
      Sleep(100);    // Sleep 100 ms in Windows
      printf(".[%d] ", i);
    }

结果是在 Windows 中每 100 毫秒出现一个带括号的数字。

在 Linux 中:

    for (int i = 0; i < 100; i++)
    {
      usleep(100000);    // Sleep 100 ms in Linux 
      printf(".[%d] ", i);
    }

结果是在 Linux 中每 100 毫秒出现一个 GROUP ob 括号内的数字。它正在运行循环,只是在睡眠完成之前不打印数字。 ????

【问题讨论】:

标签: c++ linux windows sleep usleep


【解决方案1】:

输出被缓冲。你看不到点,但它们像发条一样发出。

如果你添加

fflush(stdout); 

或输出字符串的换行符,您应该会看到点定期出现。

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 2021-08-23
    • 2013-02-11
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多