I see people doing this in some parts of heavily multi-threaded, multi-process application systems I work with. It seems to be done around debug lines:
std::cerr << "DEBUG: Reaching: " << __FUNCTION__ << " @ " << __LINE__ << std::endl;
sleep(0);
If I macro out sleep(0); (i.e. change it to “”), the debug output from the system seems to come in different order (less predictable), so I figure it makes the line come out sooner – but I thought std::cerr was unbuffered, and std::endl calls std::flush() anyway, so why would this be?
Basically, it yields control back to the scheduler, and lets you be re-scheduled instantly. That said, it’s basically a hack to try and fool the operating system into doing something.
And it’s never a good idea to fool the operating system.
If the system is suitably underloaded, then going to sleep will mean the OS gets control and lets the I/O queue flush, so it would have that effect. Sometimes. Depending.
Exactly what it’s doing depends on details of the implementation that, frankly, you can’t depend upon.