[fpc-pascal] Writeln() behaves differently on Windows and Linux, why?

Bo Berglund bo.berglund at gmail.com
Fri Jul 10 07:45:17 CEST 2020


On Fri, 10 Jul 2020 01:26:24 +0200, Tomas Hajny
<XHajT03 at hajny.biz> wrote:


Well I do not believe the problem I am seeing is because of thread
safe or not.
This test server is just running the main thread (the program itself)
where the detection of keyboard entry q is in a loop to make it
possible to end execution in a controlled way.
Nothing is output to the console from this loop when the server is
running, only after it is commanded to shut down following detection
of q.
Additionally the TIdTCPServer is created and started prior to getting
to the loop. This is for sure multi-threaded but by itself does not
interact with the console.

And I am using a TCP client application to connect to the TCP server
and it will send one message at a time to the server when connecting
in order to read back the state information for display. This is done
sequentially in a single thread on the client, so messages arrive in
an orderly fashion.

>The point is that unit Keyboard makes no changes to the console output 
>(unlike unit Crt). Unit Crt provides special features allowing to 
>perform output at a particular position of the console (which makes it 
>less reliable in multi-threaded scenarios). Without this unit, standard 
>simple I/O is used. No, it isn't guaranteed to be thread-safe per se, 
>you should make sure to wait for finishing output from the first thread 
>before you start writing in another, that's your responsibility, but 
>there are ways for achieving that (as mentioned earlier in this thread).

Instead I think that this what you mention here is the real culprit.
So Crt changes the way the console displays data on the screen as you
describe and this screen position access is probably what I see as the
messages from inside the server being output without the CR only using
LF when executing the Writeln() command.

I posted here because I thought I had discovered a bug or that I had
somehow mis-interpreted the way Writeln() works and could get an
immediate advice concerning the cause of the problem.

It is not that important since this is just a test program evaluating
the TCP server class I am building.
It would just be more convenient to stop the application using a
single keypress than two, but ths only happens while I am testing.

So by removing the loop, I can just hit Enter and it shuts down:

    Writeln('Server running, hit Enter to exit!');
    //repeat
      Read(ch);
    //until ch='q'; {q}
    Writeln('Shutting down server');

Here I use the blocking Read(ch) instead and now Enter makes the code
move on to the termination phase.
It will not be used at all in the production program...

But I really asked about the *difference* between Windows and Linux
behaviour because on Windows the original code works fine without any
screen mess.

-- 
Bo Berglund
Developer in Sweden



More information about the fpc-pascal mailing list