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';

In reply to Re: Use thread with good care, but definitely keep using it by pfaut
in thread Use thread with good care, but definitely keep using it by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.