Board index » delphi » Using Indy can mimic Erlang Like threading concepts?

Using Indy can mimic Erlang Like threading concepts?


2006-08-30 11:12:42 PM
delphi23
I'm investigating how far can Delphi mimic the Erlang aproach to
multi-threads...n However, my experience in this is, like the mayority
of the average developers, not much ;(
I have this (is the concept of how can be):
unit ParallelCode;
interface
uses
{VCL}Classes ,
{JCL}JclContainerIntf,JclQueues,JclAlgorithms,JclHashMaps;
type
TBufferType = (tbInt,tbString,tbData);
IParallelCode = interface
function GetSession(key: String): TObject;
procedure SetSession(key: String; const Value: TObject);
procedure Post(PID:Integer;Value:String);
procedure BroadCast(PidList: array of Integer;Value:String);overload;
procedure BroadCast(Value:String);overload;
procedure Execute;
procedure Get(FromPID:Integer;Value:String);
property Session[key:String]:TObject read GetSession write SetSession;
function GetIsDone: Boolean;
function GetPid: Integer;
end;
TParallelCode = class(TInterfacedObject,IParallelCode)
private
FPId : Integer;
FSession: IJclStrIntfMap;
FIsDone: Boolean;
function GetIsDone: Boolean;
function GetPid: Integer;
function GetSession(key: String): TObject;
procedure SetSession(key: String; const Value: TObject);
protected
FApply:TApplyFunction;
FValue:TObject;
procedure
DoPost(PID:Integer;Value:TStream;DataType:TBufferType);virtual;
public
procedure Execute;
procedure Post(PID:Integer;Value:String);
procedure BroadCast(PidList: array of Integer;Value:String);overload;
procedure BroadCast(Value:String);overload;
procedure Get(FromPID:Integer;Value:String);
property Session[key:String]:TObject read GetSession write SetSession;
property IsDone:Boolean read GetIsDone;
property PID:Integer read GetPid;
constructor
Create(Apply:TApplyFunction;Data:TObject;AutoStart:Boolean=True);
destructor Destroy; override;
end;
implementation
{ TParallelCode }
procedure TParallelCode.BroadCast(PidList: array of Integer; Value: String);
var
Pid:Integer;
begin
for Pid in PidList do
begin
Post(Pid,Value);
end;//for
end;
procedure TParallelCode.BroadCast(Value: String);
begin
end;
constructor TParallelCode.Create(Apply: TApplyFunction; Data:
TObject;AutoStart:Boolean=True);
begin
FSession := TJclStrIntfHashMap.Create();
FApply := Apply;
FValue := Data;
if AutoStart then
begin
Execute;
end;
end;
destructor TParallelCode.Destroy;
begin
Post(-1,'Terminado');
FSession.Clear;
FApply := nil;
FValue := nil;
inherited;
end;
procedure TParallelCode.DoPost(PID: Integer; Value: TStream;
DataType: TBufferType);
begin
end;
procedure TParallelCode.Execute;
var
value:Integer;
begin
Value := Integer(FApply(FValue));
end;
procedure TParallelCode.Get(FromPID: Integer; Value: String);
begin
end;
function TParallelCode.GetIsDone: Boolean;
begin
end;
function TParallelCode.GetPid: Integer;
begin
end;
function TParallelCode.GetSession(key: String): TObject;
begin
Result := nil;
end;
procedure TParallelCode.SetSession(key: String; const Value: TObject);
begin
end;
procedure TParallelCode.Post(PID: Integer; Value: String);
var
Bufer: TStringStream;
begin
Bufer := TStringStream.Create(Value);
DoPost(PID,Bufer,tbString);
end;
end.
So, the idea (borrowed from Erlang and clasisc web development):
Do a funcion:
function Add2(AObject: TObject): TObject;
begin
Result := TObject(Integer(AObject) + 2);
end;
Call It:
Fork( Add2, TObject(1) );
Communicate using only:
Post(ToPID=1,'Data)
and recieve messages:
procedure Get(FromPid,Data):
begin
end;
I try using Coroutine
(www.festra.com/wwwboard/messages/12775.html) and try with Indy
Fibers but I am blocked because:
a) Don't know the best aproach to send and recieve message (is using
windows messages)
b) Don't know what dangers or mainly, limitations cause the use of
Coroutine/Fibers
c) Maybe I must start using normal threads then evolve the thing?
 
 

Re:Using Indy can mimic Erlang Like threading concepts?

"mamcx" <XXXX@XXXXX.COM>writes
Quote
I'm investigating how far can Delphi mimic the Erlang aproach to
multi-threads...n However, my experience in this is, like the mayority of
the average developers, not much ;(

I try using Coroutine (www.festra.com/wwwboard/messages/12775.html)
and try with Indy Fibers but I am blocked because:

a) Don't know the best aproach to send and recieve message (is using
windows messages)

b) Don't know what dangers or mainly, limitations cause the use of
Coroutine/Fibers

c) Maybe I must start using normal threads then evolve the thing?
Cool your trying to use that code :)
you might want to try the upgrade
www.festra.com/wwwboard/messages/12899.html because the older one had
some issues that are now fixed.
You might want to look at www.festra.com/wwwboard/messages/12918.html
for my own little toy fork/join lib.
grt, Bart
 

Re:Using Indy can mimic Erlang Like threading concepts?

Quote

you might want to try the upgrade
www.festra.com/wwwboard/messages/12899.html because the older one had
some issues that are now fixed.

Interesting. Is there a little demo to show how and when to use it ?
If been reading the wiki page : en.wikipedia.org/wiki/Coroutine
and I am only beginning to understand. A little delphi demo which makes
sense would help a lot.
Thanks
 

Re:Using Indy can mimic Erlang Like threading concepts?

"theo" <XXXX@XXXXX.COM>writes
Quote
>
>you might want to try the upgrade
>www.festra.com/wwwboard/messages/12899.html because the older one
>had
>some issues that are now fixed.
>


Interesting. Is there a little demo to show how and when to use it ?

If been reading the wiki page : en.wikipedia.org/wiki/Coroutine
and I am only beginning to understand. A little delphi demo which makes
sense would help a lot.
www.festra.com/wwwboard/messages/12900.html
 

Re:Using Indy can mimic Erlang Like threading concepts?

Bart van der Werf schrieb:
Quote

www.festra.com/wwwboard/messages/12900.html


Thanks! Is there a Libc / Kylix version of it? Probably using mmap
instead of VirtualAlloc ?
 

Re:Using Indy can mimic Erlang Like threading concepts?

Excellent!
But where I get ComtecCollection and that classes?
You are interested in put this forward? I like the idea of have a easy-to-use parallering library to incorporate in MUTIS (mutis.sourceforge.net)