Hi everyone,
This is my first post for something that I could not find the answer in existing posts.
I need to run simultaneous calculations in parallel, which should print individual results via STDOUT. The following example works fine with me but I have a small problem:
When calculations are more complicated than just md5 and results are longer, threads fight for STDOUT causing interlaced/broken lines. Any ideas on how could I synchronize output so that threads don't collide?
program mt;
{$mode DELPHI}
uses {$ifdef unix} cthreads, cmem, {$endif} Sysutils, md5, classes, MTProcs;
var
q: tstringlist;
i: integer;
procedure ParallelMD5 (Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
begin
writeln('#',Index,'=',md5print(md5string(q[Index])));
end;
begin
// Load q with 10 strings
q:=tstringlist.create;
for i:=1 to 10 do q.add(inttostr(i));
// Calculate MD5 hashes in a single thread
for i:=0 to 9 do writeln ('#',i,'=',md5print(md5string(q[i])));
// Calculate MD5 hashes in 5 threads
ProcThreadPool.DoParallel(@ParallelMD5, 0, 9, nil, 5);
end.
Last, this code must run on both Linux and Windows

Thank you all in advance!