In all of these, the writing only happens after the sleep is concluded.
How could I force it to write before the sleep?
pub fn main() !void {
const outw = std.io.getStdOut().writer();
_ = try outw.write("Generic writer\n");
std.debug.print("Debug print\n", .{});
std.log.info("Log info\n", .{});
std.time.sleep(std.time.ns_per_s * 5);
}
Thanks in advance.
Well, these all should print before the sleep so this might be caused by an OS bug or stdlib bug. It might help to share your build settings and what you’re running this on, so the issue can be reproduced.
1 Like
I quickly compiled and ran your code on Windows and WSL and I see the output before the sleep. Also tested in both cmd and PowerShell 7 and both console hosts have the same behavior, and with both zig 0.12.1, 0.14.0 and master. This is probably OS/environment specific behavior like @milogreg suggests.
2 Likes
All of those calls will run the write syscalls immediately.
My guess is that you’re inadvertently/unknowingly spawning the process using something that captures the output, waits until the program exits, and then prints the output afterwards.
Either that, or your terminal or something is doing some internal output buffering, but that seems unlikely if it’s not showing up until after the full 5 second sleep.
2 Likes
FWIW I cannot reproduce this on macOS both in the VSCode terminal and Ghostty.
The text always shows up before the sleep.
I have seen similar behaviour in text without a newline though (which is then stuck in the stdout/stderr buffer, but a newline usually causes a flush).
2 Likes
Thank you very much, it really was the environment.
I was using sublime with a few plugins.
As soon as I tested it directly in the terminal, it worked as expected.
2 Likes