Recent

Author Topic: Console output and multithreading  (Read 2615 times)

zim

  • New Member
  • *
  • Posts: 17
Console output and multithreading
« on: January 16, 2024, 02:13:02 pm »
Hi
Found some strange behaviour of Writeln in multithreaded app. It's a sort of debug mode (in normal operation it's all in logs), but really annoying.
While on Windows all is ok, I expected use of critical section will help on Linux also, but not helped. Is there any working solution present ?

The problem:
Code: Pascal  [Select][+][-]
  1. var Lock: TCriticalSection;
  2. procedure Log(S: String);
  3. begin
  4.   Lock.Acquire;
  5.   try
  6.     // ... some code with log rotating and output
  7.     Writeln(S);
  8.   finally
  9.     Lock.Release;
  10.   end;
  11. end;

procedure Log is called many times from different code places, some in main thread, some from child threads.

Expected output:
log1
log2
log3

Actual output under Linux:
log1
           log2
                               log3

« Last Edit: January 16, 2024, 02:47:05 pm by zim »

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 505
Re: Console output and multithreading
« Reply #1 on: January 16, 2024, 03:12:25 pm »
I would guess it's down to the different line endings used by win (\r\n) and linux (just \n)

MarkMLl

  • Hero Member
  • *****
  • Posts: 7507
Re: Console output and multithreading
« Reply #2 on: January 16, 2024, 04:01:39 pm »
I would guess it's down to the different line endings used by win (\r\n) and linux (just \n)

"Staircasing". However I've not seen that happen despite similarly having debug WriteLn()s called by multiple threads.

i) If in doubt, put a Flush(Output) immediately after the WriteLn.

ii) I've never had to use a critical section in that circumstance.

iii) Where are you creating that critical section?

iv) I /have/ seen peculiar effects if mixing references to Output and stdout.

v) The only time I've seen staircasing like that is if I've intentionally messed around with terminal properties using ioctl().

Are you, by any chance, using something like the Crt unit elsewhere in the program? It's generally frowned upon to post a mere fragment, for precisely that reason: people can't see what you're doing before you get to the problem.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Console output and multithreading
« Reply #3 on: January 16, 2024, 04:36:03 pm »
Hi
I have "Staircasing" galore!
I do use the Crt unit(+incs) and a home-made helper-unit:
Code: Pascal  [Select][+][-]
  1. // too big, please see attachment :o)
  2.  
Funny thing is, it was running happy along ...and still does  %)
Yesterday I installed a new kernel in my lappy, upgrade from 5.16.19 to 6.5.13 After that, It all went to H*** in a handbasket when program ends?!?
It ends ok, but back at the prompt, I can't see what I'm typing, and if I type "ls -l" (blind) + enter then the staircasing goes bananas  :'(
I refuse to believe that it has to do with the kernel-upgrade. Regression to 5.16 is not an option, 'cause the new one is a lot faster  :D
What to do?
Regards Benny
« Last Edit: January 16, 2024, 04:39:56 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Console output and multithreading
« Reply #4 on: January 16, 2024, 04:42:42 pm »
Have a look-see  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 7507
Re: Console output and multithreading
« Reply #5 on: January 16, 2024, 06:08:30 pm »
What to do?

In your case    stty sane    and then start a new thread rather than hijacking this one.

You will probably need to investigate the terminal defaults and what exactly the crt unit is doing on exit. My guess is that if you'd installed that kernel version as part of a new installation there would be something else in the distro handling the terminal differently... systemd is an easy target but there's so much that you're not telling us that it's impossible to say.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1497
    • http://www.cdbc.dk
Re: Console output and multithreading
« Reply #6 on: January 16, 2024, 06:20:03 pm »
Hi
No worries Mark, I didn't want to hijack anything, just wanted to show you an example of "staircasing"...
It's the crt unit that's acting up, I'm solving it.
As you were...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 7507
Re: Console output and multithreading
« Reply #7 on: January 16, 2024, 08:40:36 pm »
No worries Mark, I didn't want to hijack anything, just wanted to show you an example of "staircasing"...
It's the crt unit that's acting up, I'm solving it.
As you were...

It's something I've seen very rarely since the days of Yggdrassil and kernel v2.x.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11732
  • FPC developer.
Re: Console output and multithreading
« Reply #8 on: January 16, 2024, 10:49:39 pm »
Crashes in the textmode IDE sometimes also cause this.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7507
Re: Console output and multithreading
« Reply #9 on: January 16, 2024, 11:01:49 pm »
Crashes in the textmode IDE sometimes also cause this.

Because the kernel hasn't been sent the ioctl() (or sequence thereof) to restore cooked mode. Except for test programs that were deliberately courting this behaviour https://github.com/MarkMLl/serialcomms/blob/main/Term.pas I've really seen this very little over the last 30 years. Except for one particular development environment based on Eclipse... I forget the details, and if they didn't take my bug report seriously then it's hardly my problem.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

zim

  • New Member
  • *
  • Posts: 17
Re: Console output and multithreading
« Reply #10 on: January 17, 2024, 12:43:15 pm »
Actually starcasing is not the only problem. Nearly at the very beginning of my log I constantly see some unicode corription also.
It's quite surprising - every time at the same place and exactly at the same character. Line is somehow broken at that character, and instead of the character an asterisk symbol is shown.
While same output written to the log files has no problems.
Forced Flush has no effect.

So, seems there're still some deep problems with console buffers and it's not thead safe.
Well, I can see the only solution - to make custom class for console output that will do in-memory buffering and a single physical output point.

 

TinyPortal © 2005-2018