Data Tree thread safe?

Hi guys,

its me again today. I encountered this problem before, but know I decided to post it here to see if anyone can give me any insights. I am converting a List<Point3d> in to a DataTree<double> The input can be very large so I want to do it via a parallel forloop in the code below, everything works fine except when I am trying to add my items to the DataTree, I get an exception message. This makes me think that DataTree might not be thread safe?

 private void RunScript(List<Point3d> input, ref object A)
    DataTree<double> dataTree = new DataTree<double>();

    int totalBranches = input.Count;

    Parallel.For(0, input.Count, i =>
      // for (int i = 0; i < input.Count; i++)
        GH_Path path = new GH_Path(i);
        double[] data = new double []{input[i].X,input[i].Y,input[i].Z};
        for (int j = 0; j < data.Length; j++)
          //dataTree.Add(data[j], path); ---> Line which bugs out

    A = dataTree;

As a general rule - Always first populate arrays when running in parallel.

After the parallel block you can add the array data to the tree. Always.

Parallel.For wouldn’t make the adding to the tree any faster even if trees were threadsafe (which they are not, just like std Lists are not either). It is the processing which has optimization potential, and arrays are inherently threadsafe (since the parallel loop will always work with unique indexes (“array slots”, so to speak).

So, add an extra loop after the Parallel.For block and add your array data to the tree. In this case I doubt there is any performance gain though, since you don’t do any “heavy” processing in the loop.

// Rolf

1 Like

Hi Rolf,

I was actually populating the Data Tree right from the input of a C# script component.

This is a very good point and I actually forgot about this important detail. Any how, I have also noticed that there are times where I have also tried to use Parallel loops in other programs. For example:

//**This code belongs to method, just extracted this part for simplicity`

       Parallel.ForEach(hashTerritory, (node) =>

                               HashSet<Cell> neighbours;
                               Cell.GetNeighbours3D(node, field3D, out neighbours);

                               bool subSet = neighbours.IsSubsetOf(hashTerritory);

                               if (subSet == false) territoryEdgeCells.Add(n);


This Parallelized ForEach loop, makes the method sometimes return territoryEdgeCells as null and sometimes not. Does this have to do with Thread Synchronization on calling the native HashSet method IsSubsetOf ?