in reply to Use thread with good care, but definitely keep using it

The principle should be: only to create a new thread, when you see it as the best way to resolve blocking. As for socket application, most of the time, you can simply resolve blocking issues by using IO::Select.
Will there be a performance issue, because using less thread? No, on a single processor computer, threading does not improve performance at all, and even on multi-processor computer, threading only improves performance when you have the right number of threads. Keep in mind, at the most, there will only be one active thread really running, on any given processor, at any given time.

That's fine if the only thing your code is blocking on is client input but if your client's request translates into disk or database I/O, IO::Select won't allow these to overlap. Threads will. Also, if a client request turns into a long cpu-bound calculation, using threads will allow progress to be made (and possibly reported back to the client to show activity) on all outstanding requests simultaneously. Without threads, your clients would be processed serially and think nothing is happening.

Threading is just another tool you can use to solve performance problems. There's no one tool that solves all problems. Before choosing a tool, make sure you understand the tool, its limits, and the problem you're using it on. Make sure the tool fits the situation.

--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re: Re: Use thread with good care, but definitely keep using it
by pg (Canon) on Mar 14, 2003 at 02:09 UTC
    Agreed.

    Of course there are situations threading is the best solution. (The only reason I particularly mentioned IO::Select, is just a direct thought concerning that thread I mentioned in my original post.)

    I think, whenever threading might be a good solution, we should give it a try. At the same time, design carefully. Perl thread is still experimental, but if nobody use it, how can we gather results, gain experience, and provide constructive feedback to the the designers of Perl (itself).

    I expect lots of different replies to this post, some might even sound directly against each other. But if we consider the fact that, each reply has a different focus, you would realize that those differences actually support each other, help to form the entire picture.

    Agree with you that, just like any other tools or functions in Perl, you have to use it wisely to resolve the "right" problems.