c++linuxwindowssleepusleep

SLEEP: (Sleep or usleep) is not suspending everything in my thread in Linux but it does in Windows? why?


In Windows:

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

The result is a bracketed number comes out every 100ms in Windows.

In Linux:

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

The result is a GROUP ob bracketed number comes out every 100ms in Linux. It is running the loop, just not printing out the numbers until sleep is done. ????


Solution

  • The output is buffered. You don't see the dots, but they are issued like clockwork.

    If you add

    fflush(stdout); 
    

    or a newline to the output string, you should see the dots appear regularly.