Thank you.
This literal line has worked, after I have inserted it to my code. Though, is
output a file descriptor for
stdout?
I have already tried to use this procedure before your suggestion, but it didn't work, because I wrote
stdout between parentheses.
This page suggests that flush's parameter is a text file descriptor, as far as I understand.
And why you'd expect nice output from threads without some rudimentary synchronization?
If we are talking about the example, I have expected
writeln(); invocation to be atomic. Suppose two threads call for writeln in same time, what is going to happen? I don't think, having random mix of chars from one and other thread makes any sense. And if
writeln is atomic, then it makes sense that if it outputs a complete line, it won't get mangled in the resulting output, only it might get between lines output by different threads, depending on synchronization.
No, we have decided to provide each thread with its own buffer, cause then the RTL does not need to do any synchronization. This was done by choice and won't be changed.
If you want uncorrupted output per thread then you need to synchronize yourself and use Flush in each thread to ensure that the output buffer is flushed.
Well, if every writeln call had to be followed by
Flush(), then it's going to be terribly inefficient, and what's worse, what if threads switch between
writeln and
Flush?
I have another suggestion in this case. What if every thread flushed its buffer if the next line cannot fit within it, instead of splitting the line between two blocks? And if
write/writeln output is bigger than block size, then it should flush both before and after this command automatically.
This way, while order of lines between threads isn't guaranteed to be preserved without additional code, at least complete lines won't be arbitrarily broken. I think that output should not depend on whenever it's done onto the terminal directly or through a pipe to
tee,
less,
grep or whatever.
Presumably going directly to the filehandle- which is a per-process rather than a per-thread resource- would work as OP expected.
Did you mean, that if I use
writeln(output, 'some message'); on place of every
writeln('some message'), it should solve the problem? I have tried, it didn't work.