I have a tree data structure. In addition to the normal insert and removal functions I have a COMPUTE function which computes certain values from the nodes present in the tree. The insert and removal functions affect the tree. But the COMPUTE function does not modify the tree. The COMPUTE function has its own internal queues and adds and deletes nodes to these queues. Here I present a simplified pseudocode of the compute function:
class tree{
vector<int> value;
tree * children
}
tree::COMPUTE(vector<int> inputValue)
{
Set currentSet;
Set nextSet
currentSet.add(root);
foreach (node in currentSet)
{
if(node has children)
foreach(childNode of node)
{
if(CONDITION_SATISFIED(childNode))
add childNode to nextSet;
}
else
output childNode
}
}
So the COMPUTE function simply recurses through the tree, adds nodes to its own internal set datastructure and does some computations.
Now I want to multithread the COMPUTE function. Since it does not modify the tree, I guess this is easily doable. Each thread gives its own value and gets its own COMPUTE output. The code is written in C++.
My question is this:
If I call it like this:
void * threadRoutine
{
tree.COMPUTE(thisThreadVal);
}
int main()
{
tree myTree();
//Insertion Code here
call N threads each with different parameters but the same tree.
}
I dont this it is correct. This is because all the threads will call the same member function of the same tree object. So the internal data strcutures like the sets used in the COMPUTE method will be mangled.
I think I should write the compute function outside the tree and not as a member function of the tree. Can somebody tell me if this is right.
btw: In case somebody is curious, the exact tree that I will be using is called a Cover Tree. It is a relatively new data structure used for finding nearest neighbour queries.