开发者

Recursion within a thread

Is it a good idea to call a recursive function inside a thread ? I am creating 10 threads, the thread function in turn call a recursive function . The bad part is

ThreadFunc( )
{
   for( ;condn   ;   )
     recursive开发者_运维知识库Func(objectId);
}


bool recursiveFunc(objectId)
{
    //Get a instance  to the database connection

    // Query for attibutes of this objectId

    if ( attibutes satisfy some condition)
        return true;
    else
        recursiveFunc(objectId)   // thats the next level of objectId
}

The recursive function has some calls to the database My guess is that a call to recursive function inside a loop is causing a performance degradation. Can anyone confirm


Calling a function recursively inside a thread is not a bad idea per se. The only thing you have to be aware of is to limit the recursion depth, or you may produce a (wait for it...) stack overflow. This is not specific to multithreading but applies in any case where you use recursion.

In this case, I would recommend against recursion because it's not necessary. Your code is an example of tail recursion, which can always be replaced with a loop. This eliminates the stack overflow concern:

bool recursiveFunc(objectId)
{
    do
    {
        // Get an instance to the database connection

        // Query for attributes of this objectId

        // Update objectId if necessary (not sure what the "next level of objectId" is)
    }
    while(! attributes satisfy some condition);

    return true;
}


There's no technical reason why this wouldn't work - it's perfectly legal.

Why is this code the "bad part"?

You'll need to debug/profile this and recursiveFunc to see where the performance degradation is.

Going by the code you've posted have you checked that condn is ever satisfied so that your loop terminates. If not it will loop for ever.

Also what does recursiveFunc actually do?

UPDATE

Based on your comment that each thread performs 15,000 iterations the first thing I'd do would be to move the Get an instance to the database connection code outside recursiveFunc so that you are only getting it once per thread.

Even if you rewrite into a loop (as per Martin B's answer) you would still want to do this.


It depends on how the recursive function talks to the database. If each (or many) level of recursion reopens the database that can be the reason for degradation. If they all share the same "connection" to the database the problem is not in recursion but in the number of threads concurrently accessing the database.


The only potential problem I see with the posted code is that it can represent an infinite loop, and that's usually not what you want (so you'd have to force break somewhere on known reachable conditions to avoid having to abend the application in order to break out of the thread (and subsequently the thread).

Performance degradation can happen with both threading, recursion, and database access for a variety of reasons. Whether any or all of them are at fault for your problems is impossible to ascertain from the little you're showing us.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜