Recursive or Threads
Considering many problems where we use recursive functions such as traversing through tree structures.. it it better to use threads or recursive function itself.. WHich is faster.. and what are the advantages and disadvantages...
//Recursive function to display nodes in binary tree...
Is the above better or instead creating a new thread every time instead of calling the function better...
I'm pretty sure this would depend on the o/s's despatching model; but I'd guess on most current implementations that creating threads unnecessarily is a resource waste unless you planned to spend a significant time within them.
You might find this site interesting.
Thanx for the link.. it is interesting...
But if i wanted to run the recursive in an order say.. First display left node, then right etc... Do you think this will be messed up using threads.. Because each thread execution in not controlled by the other thread in general...
My above algorithm will first display all the left nodes and so on.. But will the same happen if i use threads....
And how are the two represented into the memory.. i think recursive functions are representated as stracks into the memory.. SO dont you think threads should be faster...
IMHO, this is not a good use for threads. The set-up, execution, and cleanup would both make the function(?) unreliable and wasteful. Now if you wished to run a normal tree-traversal on a separate thread, this might be useful if the tree is extremely large...
I think you would definitly screw up the input on a console since
the order your printing them out could be different.
Also cout would probably tie up the input stream. If
say you were calculating the weighted sum
of all nodes in a tree.
You could actually split into two processes at
root->left and root->right. I think this might be faster on a very
large tree and given a multiprocessor machine.