[fpc-pascal] writeln in threads on linux

Florian Klaempfl florian at freepascal.org
Sun Jul 2 12:45:42 CEST 2006


Ole J. R√łtne wrote:
>  
> Can anyone explain why i get output like this in a small test program:
> Inside Thread
> 		 Main Loop
> Inside Thread
> 		 Main Loop
> Inside Thread
> 		 Main Loop
> 
> 
> On Windows the output are lined up just as I would expect:
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
> 
> Here's a snip of the code:
> 
> // Main Prog
> 
> Thr := threadtest.Create;
> 
> Repeat
>   k := PollKeyEvent;
>   WriteLn('Mein Loop');
>   Sleep(100);
> Until k <> 0;
> 
> And the ThreadTest.Execute i just:
> 
> Procedure ThreadTest.Execute;
> Begin
>   Repeat
>     WriteLn('Inside Thread');
>     Sleep(100);
>   Until Terminated
> End;
> 
> 
> And if this is a "wrong way" of doing this thing, could someone tell me how
> it should be..

You probably want to guard the output with mutexes or semaphores.
Writing unsynchronized to a stream has always unexpected results.

> 
> Oh and BTW. Using FPC 2.1.1 snapshot from 14.06.06 (from Lazarus site), on
> OpenSuSe 10.1..
> 
> 
> 
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal




More information about the fpc-pascal mailing list