Concurrent computing: Difference between revisions

→‎{{header|Pascal}}: Re-add Pascal entry. My apologies if I messed up the formatting
(Revert to revision as of November 5)
(→‎{{header|Pascal}}: Re-add Pascal entry. My apologies if I messed up the formatting)
Line 1,449:
 
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}}
<lang perl>use threads;
use Time::HiRes qw(sleep);
 
$_->join for map {
threads->create(sub {
sleep rand;
print shift, "\n";
}, $_)
} qw(Enjoy Rosetta Code);</lang>
 
Or using coroutines provided by {{libheader|Coro}}
<lang perl>use fe
 
=={{header|Perl}}==
Line 1,464 ⟶ 1,554:
Or using coroutines provided by {{libheader|Coro}}
<lang perl>use feature qw( say );
use Coro;
use Coro::Timer qw( sleep );
 
$_->join for map {
async {
ature qw( say );
use Coro;
use Coro::Timer qw( sleep );
10,327

edits