Concurrent computing: Difference between revisions

→‎{{header|Perl}}: added header pascal {trans|Delphi}}
m (Updated code to match task, removed output as defined by task.)
(→‎{{header|Perl}}: added header pascal {trans|Delphi}})
Line 1,233:
See also [http://pari.math.u-bordeaux1.fr/Events/PARI2012/talks/pareval.pdf Bill Allombert's slides on parallel programming in GP].
 
=={{header|Pascal}}==
{{trans|Delphi}} modified for linux. Using simple running thread-counter to circumvent WaitForMultipleObjects.<BR>
Output of difference of sleep time and true sleep time ( running with 0..1999 threads you see once a while 1)
<lang pascal>program ConcurrentComputing;
{$IFdef FPC}
{$MODE DELPHI}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils, Classes;
 
type
TRandomThread = class(TThread)
private
FString: string;
T0 : Uint64;
protected
procedure Execute; override;
public
constructor Create(const aString: string); overload;
end;
const
MyStrings: array[0..2] of String = ('Enjoy ','Rosetta ','Code ');
var
gblRunThdCnt : NativeInt = 0;
 
constructor TRandomThread.Create(const aString: string);
begin
inherited Create(False);
FreeOnTerminate := True;
FString := aString;
inc(gblRunThdCnt);
end;
 
procedure TRandomThread.Execute;
var
i : NativeInt;
begin
i := Random(300);
T0 := GettickCount64;
Sleep(i);
//output of difference in time
Writeln(FString,i:4,GettickCount64-T0 -i:2);
dec(gblRunThdCnt);
end;
 
var
lThreadArray: Array[0..9] of THandle;
i : NativeInt;
begin
Randomize;
gblRunThdCnt := 0;
For i := low(lThreadArray) to High(lThreadArray) do
lThreadArray[i] := TRandomThread.Create(Format('%9s %4d',[myStrings[Random(3)],i])).Handle;
while gblRunThdCnt > 0 do
sleep(125);
end.</lang>
{{out}}
<pre>
Enjoy 4 16 0
Code 0 22 0
Code 1 32 0
Rosetta 7 117 0
Enjoy 2 137 0
Code 6 214 0
Code 5 252 0
Enjoy 3 299 0</pre>
=={{header|Perl}}==
{{libheader|Time::HiRes}}
Anonymous user