Recent

Author Topic: Threadpool ...  (Read 4577 times)

aminer

  • Hero Member
  • *****
  • Posts: 956
Threadpool ...
« on: March 01, 2010, 06:08:43 pm »

Hello,

As you have noticed my Threadpool, see:
http://pages.videotron.com/aminer/

use something like

procedure TThreadPoolThread.Execute;
begin
while not Terminated do
begin
end;
end;

Next step i will enhance my Threadpool to use an event object
to not consume any CPU if there is no objects in the queue.
And also i will add another enhancement , you will be able to call
any method with the execute(pointer(obj),method);
and also threadpool will use ParallelQueue (a lockfree mpmc).

Also, look carefully inside the lockfree_spmc zipfile at:

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

In the POP method i am doing this:

1-function TLockfree_SPMC.pop(var obj:tNodeQueue):boolean;
2- var
3- lastHead : longword;
4- begin
5- repeat
6- if tail[0]<>head[0]
7- then
8- begin
9- lastHead:=head[0];
10- obj:=getObject(integer(lastHead));
11- if CAS(head[0],lasthead,lasthead+1)
12- then
13- begin
14- result:=true;
15- exit;
16- end;


If you have noticed at line 6 i am testing if
tail[0]<>head[0] and after that i am copying
head[0] to the local variable lasthead, so, if
between line 6 and 9 head[0] has changed and is
equal to tail[0] , that means that the queue is empty
and the thread must *NOT* pop the object from the queue !
Hence, the correct logic is: we have to copy head[0] to the
local variable and AFTER that we test if tail[0]<>head[0]

So, here is my patch to the pop method:

---------------------------------------------------------------------------­-----
function TLockfree_MPMC.pop(var obj:tNodeQueue):boolean;
var lastHead : longword;
begin
repeat
lastHead:=head[0];
if tail[0]<>head[0]
then
begin
obj:=getObject(integer(lastHead));
if CAS(head[0],lasthead,lasthead+1)
then
begin
result:=true;
exit;
end;
end
else
begin
result:=false;
exit;
end;
until false;
end;
-------------------------------------------------------------------------

And also if you have noticed, there is a test with a
CAS(head[0],lasthead,lasthead+1) , so, if head[0] have
changed the CAS will fail.. hence, there is no such
thing that ressemble the ABA problem...
And the logic is correct.

Please patch and change the po() method in lockfree_spmc.pas
inside the threadpool zipfile

I will soon patch the algorithm and upload the zipfile to my
website...

Sincerely,
Amine Moulay Ramdane.





 

TinyPortal © 2005-2018