Threadpool 1.2 (stable version ...) ...

Threadpool 1.2 (stable version ...) ...

Hello all,

I have updated Threadpool (and threadpool with priority) to version

1.2 - a stable version - i had some - difficult - problems with the

FPC memory manager, so, i have used some tricks to avoid

completly those problems..

Author: Amine Moulay Ramdane

Language: FPC Pascal v2.2.0+ / Delphi 5+: http://www.freepascal.org/

Operating Systems: Win , Linux and Mac (x86).

Description:

Lock-free threadpool.

The following have been added:

-- Lockfree ParallelQueue for less contention and more efficiency

or it can use lockfree_mpmc - flqueue that i have modified, enhanced

and improved... -

- It does use many queues and work-stealing - for more efficiency -

- Enters in a wait state when there no job in the queue,

hence, it's very efficient

- You can now call any method with the excute() method, like this

excute(your method, parameter);

In Object Pascal it looks like this:

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

- look inside the zipfile i have included two demos:

test.pas, and ptestpool.pas - a Parallel program of Matrix

multiply by a vector that use SSE+ -

Look into defines.inc there is many options:

CPU32: for 32 bits architecture

MUTIPLE_PRODUCER: mutiple producer (threads)

ParallelQueue: does use ParallelQueue - very efficient -

Lockfree_MPMC: does use Lockfree_MPMC

ParallelQueueh: does use ParallelQueueh - ParallelQueueh is for educational purpose -

SINGLE_PRODUCER: for a single producer (thread)

Required FPC switches: -Sd -dFPC -dWin32 -dFreePascal

-Sd for delphi mode....

Required Delphi switches: -DMSWINDOWS -DDelphi -$H+

Please look at the examples test.pas,testpool.pas and test_thread.pas...

Note: testpool.pas does require Delphi 5+, test.pas and test_thread.pas

works with both FreePascal and Delphi

You can download the new and stable version from:

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

My Threadpool is VERY easy to use, and here is an example:

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

program test;

uses

{$IFDEF Delphi}

cmem,

{$ENDIF}

ThreadPool,sysutils,windows,classes,syncobjs;

type

TMyThread = class (TThreadPoolThread)

//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: TThreadPool;

obj:pointer;

cs:TCriticalSection;

number:integer;

procedure TMyThread.MyProc1(obj: Pointer);

begin

cs.enter;

inc(number);

if number=4000000 then event2.setevent;

writeln(number);

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

cs.leave;

end;

procedure TMyThread.MyProc2(obj: Pointer);

begin

cs.enter;

inc(number);

if number=4000000 then event2.setevent;

writeln(number);

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

cs.leave;

end;

function StartFunc1(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

func:TMyProc;

begin

event1.waitfor(INFINITE);

for i:=0 to 999999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc1,obj);

end;

end;

function StartFunc2(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

func:TMyProc;

begin

event1.waitfor(INFINITE);

for i:=0 to 999999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc1,obj);

end;

end;

function StartFunc3(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

func:TMyProc;

begin

event1.waitfor(INFINITE);

for i:=0 to 999999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc2,obj);

end;

end;

function StartFunc4(InVal:pointer):DWORD;

var temp:tobject;

i:integer;

obj:pointer;

func:TMyproc;

begin

event1.waitfor(INFINITE);

for i:=0 to 999999

do

begin

obj:=pointer(i);

TP.execute(myobj.myproc2,obj);

end;

end;

begin

number:=0;

cs:=TCriticalSection.create;

myobj:=TMyThread.create;

TP := TThreadPool.Create(4, 10, TMyThread);

// 4 workers threads and 2^10 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(2, @hndlArr, True, INFINITE);

event2.waitfor(INFINITE);

writeln('Number is: ',number);

end.

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

Sincerely,

Amine Moulay Ramdane.

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.