0

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. ????

jdl
  • 6,151
  • 19
  • 83
  • 132
  • Try sleep command on Windows from [here](https://stackoverflow.com/a/17283549/5517378). Maybe your sleep comand in Windows doesn't interpret 100 as milliseconds but rather as a seconds. – NutCracker Jul 01 '19 at 21:54
  • 5
    I'd say the printf on Linux is getting buffered, so it only prints once that buffer is filled up, you can test by calling fflush(stdout); after the printf You could also disable buffering beforehand with setbuf(stdout, NULL); – Ed Lambert Jul 01 '19 at 22:07
  • 1
    My guess would be you need to flush - `fflush(stdout);` (And by the way, `std::this_thread::sleep_for(std::chrono::milliseconds{100});` should be more portable.) – Daniel Schepler Jul 01 '19 at 22:07
  • 2
    Possible duplicate of [Why does printf() not print anything before sleep()?](https://stackoverflow.com/q/338273/608639), [C sleep function not working](https://stackoverflow.com/q/13568388/608639), [Printf in C with using sleep not working](https://stackoverflow.com/q/27077505/608639), [C sleep method obstructs output to console](https://stackoverflow.com/q/28351161/608639), [Output not displayed with usleep until a line break is given](https://stackoverflow.com/q/16142384/608639), etc. – jww Jul 02 '19 at 01:12

1 Answers1

1

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.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42