BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using Indy can mimic Erlang Like threading concepts?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
mamcx
Guest





PostPosted: Wed Aug 30, 2006 8:12 pm    Post subject: Using Indy can mimic Erlang Like threading concepts? Reply with 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 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
(http://www.festra.com/wwwboard/messages/12775.html) and try with Indy
Fibers but I'm 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?
Back to top
Bart van der Werf
Guest





PostPosted: Thu Aug 31, 2006 2:12 pm    Post subject: Re: Using Indy can mimic Erlang Like threading concepts? Reply with quote



"mamcx" <this (AT) notexist (DOT) com> wrote in message
news:44f5aaeb (AT) newsgroups (DOT) borland.com...
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 (http://www.festra.com/wwwboard/messages/12775.html)
and try with Indy Fibers but I'm 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
http://www.festra.com/wwwboard/messages/12899.html because the older one had
some issues that are now fixed.

You might want to look at http://www.festra.com/wwwboard/messages/12918.html
for my own little toy fork/join lib.

grt, Bart
Back to top
theo
Guest





PostPosted: Thu Aug 31, 2006 6:13 pm    Post subject: Re: Using Indy can mimic Erlang Like threading concepts? Reply with quote



Quote:

you might want to try the upgrade
http://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 : http://en.wikipedia.org/wiki/Coroutine
and I'm only beginning to understand. A little delphi demo which makes
sense would help a lot.

Thanks
Back to top
Bart van der Werf
Guest





PostPosted: Thu Aug 31, 2006 6:32 pm    Post subject: Re: Using Indy can mimic Erlang Like threading concepts? Reply with quote

"theo" <nospam (AT) for (DOT) me> wrote in message
news:44f6e107$1 (AT) newsgroups (DOT) borland.com...
Quote:

you might want to try the upgrade
http://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 : http://en.wikipedia.org/wiki/Coroutine
and I'm only beginning to understand. A little delphi demo which makes
sense would help a lot.

http://www.festra.com/wwwboard/messages/12900.html
Back to top
theo
Guest





PostPosted: Thu Aug 31, 2006 8:21 pm    Post subject: Re: Using Indy can mimic Erlang Like threading concepts? Reply with quote

Bart van der Werf schrieb:

Quote:

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



Thanks! Is there a Libc / Kylix version of it? Probably using mmap
instead of VirtualAlloc ?
Back to top
mamcx
Guest





PostPosted: Thu Aug 31, 2006 8:45 pm    Post subject: Re: Using Indy can mimic Erlang Like threading concepts? Reply with quote

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)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.