Mega Code Archive

 
Categories / Delphi / Examples
 

Threadpool 1.14

Title: Threadpool 1.14 Question: What's Threadpool ? Answer: Hello all, 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... - - 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 , priority); In Object Pascal it looks like this: TP.execute(myobj.myproc1,pointer(obj),NORMAL_PRIORITY); (look inside the zipfile i have included 3 demos: test.pas, ptestpool.pas and test_thread.pas - a Parallel program of Matrix multiply by a vector that use SSE+ - Look into defined.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_SPMC ParallelQueueh: does use ParallelQueueh - ParallelQueueh is for educational purpose - SINGLE_PRODUCER: for a single producer (thread) Required switches: -Sd (Delphi mode) for FPC Please look at the examples test.pas and testpool.pas inside the zip... Note: testpool.pas does require Delphi 5+, test.pas works with both FreePascal and Delphi You can download Threadpool 1.1 from: http://pages.videotron.com/aminer/ Language: FPC Pascal v2.2.0+ / Delphi 5+: http://www.freepascal.org/ Operating Systems: Win , Linux and Mac (x86). Threadpool is *VERY* easy to use, here is an example: ----------------------------------------------------------------------------------- program test; uses {$IFDEF Delphi} cmem, {$ENDIF} ThreadPool,sysutils,syncobjs; {$I defines.inc} type TMyThread = class (TThreadPoolThread) //procedure ProcessRequest(obj: Pointer); override; procedure MyProc1(obj: Pointer); procedure MyProc2(obj: Pointer); end; var myobj:TMyThread; TP: TThreadPool; obj:pointer; cs:TCriticalSection; procedure TMyThread.MyProc1(obj: Pointer); begin cs.enter; writeln('This is MyProc1 with parameter: ',integer(obj)); cs.leave; end; procedure TMyThread.MyProc2(obj: Pointer); begin cs.enter; writeln('This is MyProc2 with parameter: ',integer(obj)); cs.leave; end; begin myobj:=TMyThread.create; cs:=TCriticalSection.create; TP := TThreadPool.Create(4, 20, TMyThread); // 4 workers threads and 2^20 items for each queue. obj:=pointer(1); TP.execute(myobj.myproc1,pointer(obj)); obj:=pointer(2); TP.execute(myobj.myproc2,pointer(obj)); readln; TP.Terminate; TP.Free; end. ---------------------------------------------------------------------------- Sincerely, Amine Moulay Ramdane.