thread

Discussion in 'Perl Misc' started by Larry, Nov 8, 2007.

  1. Larry

    Larry Guest

    Hi peeps,

    below is the code of a script that spawns a new thread whenever
    it gets a new request:

    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use IO::Socket::INET;
    use Data::Dumper;
    use threads;
    use IO::Handle;
    STDOUT->autoflush(1);

    my $sock = IO::Socket::INET->new(
    LocalPort => '65001',
    Proto => 'tcp',
    Reuse => 1,
    Listen => 1,
    Type => SOCK_STREAM
    ) || die $!;

    $sock->listen();

    while ( my $client = $sock->accept() )
    {

    if ($client)
    {
    $client->autoflush(1);

    my $thr = threads->new(\&manage, $client);

    for my $t (threads->list()) {
    printf Dumper(\$t) . " has tid = %d\n", $t->tid();
    }
    }
    }

    sub manage {

    my $client = shift;
    my $data = <$client>;
    print "$data\n";
    sleep 5;
    syswrite $client, "Hello World!";
    close($client);

    }

    __END__;

    now, I'd like to have all the threads killed before a new thread gets
    spawnd...can it actually be done??

    thanks
     
    Larry, Nov 8, 2007
    #1
    1. Advertising

  2. Larry

    Guest

    Larry <> wrote:

    ....

    > now, I'd like to have all the threads killed before a new thread gets
    > spawnd...can it actually be done??


    I don't know if it can be done, but if you only want to have one thread
    running at a time (well, two, one that is doing stuff and one that is
    killing off the one that is doing stuff), why use threads in the first
    place?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Nov 9, 2007
    #2
    1. Advertising

  3. Larry

    Larry Guest

    In article <20071109130443.976$>,
    wrote:

    > I don't know if it can be done, but if you only want to have one thread
    > running at a time (well, two, one that is doing stuff and one that is
    > killing off the one that is doing stuff), why use threads in the first
    > place?


    I cannot wait for others threads to finish ... anyway ... I need to get
    control of <STDIN> ...
     
    Larry, Nov 9, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,586
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    540
  3. Stephen Miller
    Replies:
    3
    Views:
    4,033
    Stephen Miller
    Jul 2, 2004
  4. Johanna
    Replies:
    0
    Views:
    623
    Johanna
    Oct 13, 2004
  5. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    2,003
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
Loading...

Share This Page