The first thing you need to fix is this:

lock %{ $cnf{'lock'} };

(Hint: enabling warnings would tell you this!)

You never create a key in %cnf called 'lock'. (And when you do, it would have to contain a reference to a shared variable for that to work.)

Beyond that, there are several pretty dubious pieces of code:

  1. You have subroutines nested inside subroutines.

    Which almost certainly doesn't do what you think it does, and just serves to confuse things.

  2. Updated in the light of Corion's post below. It seems that END() subs will be treated as END{} blocks...though the sub annotation seems to serve little purpose other than to confused. Making END-blocks compatible with ancient versions of Perl seems to serve little purpose in a program that uses threads.

    You appear to think that your sub END will get called automatically (as you never call it directly), but that is not the case.

    END{} blocks get called automatically, not subs called END().

  3. You have
    # fix me, not work in win32, why? #$SIG{'INT'} = $SIG{'TERM'} = sub { lock %stat; $stat{'done'}++ };

    What do you mean by "not work in win32"?

    If uncommented, that statement (under win32) does install signal handlers for 'INT' and 'TERM' that do get called. So, in what way "not work"?

  4. You also have
    # fix me, the thread blocks here, why? # if I lock %{ $cnf{'lock'} } untill close the $c, things +will be OK, # but then we can't gain advantage of the multi-threads no +r the pre-threads # design. if ( my $r = $c->get_request() ) {

    It is not at all clear what you mean by that?

    To get to that point, this thread has accepted a connection and is now about to fetch the http request that connecting client made. How could it block?

Overall, your post poses no questions. The source code contains several 'fix me' annotations, but the details are terse and sketchy and seem to reflect your misunderstandings more than problem descriptions that can be addressed.

To progress this, you're going to have to detail each of the problems explaining what you think should happen and what actually happens.

You should also explain the format of the urls the clients will send, because expecting us to reverse engineer them from the code is pushing your luck.

In general, dumping a big chunk of code with a few embedded "fix me"s, is not a great way to go about getting help.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: multi thread problem in an simple HTTP server by BrowserUk
in thread multi thread problem in an simple HTTP server by bravesoul

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.