Community
cancel
Showing results for 
Search instead for 
Did you mean: 
aminer10
Beginner
30 Views

Threadpool 1.14 ...


Hello all,


I think we don't need an array of critical sections...

You can download the new version 1.14 of both
Threadpool and Threadpool with priority from:

http://pages.videotron.com/aminer/


I have included a test_thread.pas example
- with many workers threads ...- .

This patch avoids deadlock...

TPThreadPool.execute() method have changed to...

--------------------------------------------------------------------------------------------------
function TThreadPool.execute(func:TmyProc;const Context: Pointer): Boolean;
var
params: MyParam;
local_balance,local_count:integer;
begin
params:=MYParam.create;
params.proc:=func;
params.param:=context;

local_balance:=LockedIncLong(balance1) mod FThreadCount;
local_count:=Queues[local_balance].count;
while not Queues[local_balance].push(TObject(params)) do;
if local_count=0 then events[local_balance].setevent;
end;

---------------------------------------------------------------------------------------------


Thank you for your time...


Sincerely,
Amine Moulay Ramdane.

0 Kudos
1 Reply
aminer10
Beginner
30 Views


Hello,


Threadpool is very easy to use...

Here is an example with priorities - you can
giveLOW_PRIORITY, NORMAL_PRIORITY
and HIGH_PRIORITY - look attest_thread.pas
inside the pthreadpool.zip - that i have just wrote
for you, that use many workers threads..

Note: don't forget to give some feedback...

--------------------------------------------------------

program test;

uses

{$IFDEF Delphi}

cmem,

{$ENDIF}

PThreadPool,sysutils,windows,classes,syncobjs;

type

TMyThread = class (TPThreadPoolThread)

//procedure ProcessRequest(obj: Pointer); override;

procedure MyProc1(obj: Pointer);

procedure MyProc2(obj: Pointer);

end;

var hndlArr : Array[0..19] of THandle;

AId:DWORD;

event1,event2:TSimpleEvent;

n:int64;

j:integer;

myobj:TMyThread;

TP: TPThreadPool;

obj:pointer;

cs:TCriticalSection;

number:integer;

procedure TMyThread.MyProc1(obj: Pointer);

begin

cs.enter;

inc(number);

if number=8000 then event2.setevent;

//writeln('This is MyProc1 with parameter: ',integer(obj));

cs.leave;

end;

procedure TMyThread.MyProc2(obj: Pointer);

begin

cs.enter;

inc(number);

if number=8000 then event2.setevent;

//writeln('This is MyProc2 with parameter: ',integer(obj));

cs.leave;

end;

function StartFunc1(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

begin

event1.waitfor(INFINITE);

for i:=0 to 1999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc1,obj,NORMAL_PRIORITY);

end;

end;

function StartFunc2(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

begin

event1.waitfor(INFINITE);

for i:=0 to 1999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc1,obj,NORMAL_PRIORITY);

end;

end;

function StartFunc3(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

begin

event1.waitfor(INFINITE);

for i:=0 to 1999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc2,pointer(obj),NORMAL_PRIORITY);

end;

end;

function StartFunc4(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

begin

event1.waitfor(INFINITE);

for i:=0 to 1999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc2,pointer(obj),NORMAL_PRIORITY);

end;

end;

begin

number:=0;

cs:=TCriticalSection.create;

myobj:=TMyThread.create;

TP := TPThreadPool.Create(4, 20, TMyThread); // 4 workers threads and 2^20 items for each queue.

event1:=TSimpleEvent.create;

event2:=TSimpleEvent.create;

hndlArr[0]:=BeginThread(nil,0,@StartFunc1,pointer(1),0,AId);

hndlArr[1]:=BeginThread(nil,0,@StartFunc2,pointer(1),0,AId);

hndlArr[2]:=BeginThread(nil,0,@StartFunc3,pointer(1),0,AId);

hndlArr[3]:=BeginThread(nil,0,@StartFunc4,pointer(1),0,AId);

event1.setevent;

WaitForMultipleObjects(4, @hndlArr, True, INFINITE);

event2.waitfor(INFINITE);

writeln('Number is: ',number);

end.

---------------------------------------------------------




Sincerely,
Amine Moulay Ramdane.



Reply