Thread: Recursive or Threads

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    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...


    For example

    Code:
    //Recursive function to display nodes in binary tree...
    displaynode(node *temp)
    {
    
    cout<<temp->val;
    
    if(temp->left!=NULL)
    displaynode(temp->left);
    
    if(temp->right!=NULL)
    displaynode(temp->right);
    
    
    }

    Is the above better or instead creating a new thread every time instead of calling the function better...

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    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.
    Joe

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You might find this site interesting.
    http://supertech.lcs.mit.edu/cilk/

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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...

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  3. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM