[fpc-devel] TAVLTree(avl_tree.pp) thread safety : second proposition

Inoussa OUEDRAOGO inoussa12 at gmail.com
Thu Aug 7 13:30:12 CEST 2008


Second proposition :

Another proposition is to introduce a base avl class say TBaseAVLTree
that defines two abstract virtual methods "NewNode()" and "FreeNode()"
and then define :
 - TAVLTree : that _do not_ use a node mem manager, then thread safe.
 - TAVLManagedTree that uses a node mem manager provided in the constructor.
   The developer using this one is _aware_ of the thread safety issue and can
   then provide a thread safe node mem manager for an instance exposed
   to more than one thread while using the default not thread safe node
   mem manager for a thread local instance.


For the record, even if each thread has a non-thread-safe node mem manager
the issue remains : ThreadA create a avl and populate it. ThreadB
delete some nodes of that avl. The deleted nodes have been created
using ThreadA's node mem manager while collected by ThreadB's node mem manager.
ThreadA and ThreadB could be using different node mem manager implementations.



TBaseAVLTree = class
...
protected
 function NewNode() : TAVLTreeNode;virtual;abstract;
 procedure FreeNode(ANode : TAVLTree);virtual;abstract;
...
end;

TAVLTree = class(TBaseAVLTree)
protected
 function NewNode() : TAVLTreeNode;override;
 { implemented as
   begin
     Result:=TAVLTreeNode.Create();
   end;}

 procedure FreeNode(ANode : TAVLTree);override;
 { implemented as
   begin
     if ( ANode <> nil ) then
       ANode.Free();
   end;}
end;

TAVLManagedTree = class(TBaseAVLTree)
private
 FNodeMemManager : TAVLTreeNodeMemManager;
protected
 // these implementations use FNodeMemManager
 function NewNode() : TAVLTreeNode;override;
 procedure FreeNode(ANode : TAVLTree);override;
public
 constructor Create(OnCompareMethod: TListSortCompare;
ANodeMemManager : TAVLTreeNodeMemManager);
end;



-- 
Inoussa O.



More information about the fpc-devel mailing list