Hi all,

Does anyone know if it is possible for a thread to create its own child threads? All the threads that are created seem to be tied to the main thread, regardless of the thread they were created from.

I've tried:
threads->self->create

But it's no go either. Complete test code is attached.

Any confirmation on this is appreciated. Thanks.
use strict;
use warnings;
use threads;
use threads::shared;

$|=1;

my %command : shared = (1=>'',2=>'');

my $thread = threads->create(\&startThread,1);
my $thread2 = threads->create(\&startThread,2);
sleep(5);

while (1)
{
        print "pinging threads\n";
        {
                lock %command;
                $command{1} = 'PING';
                $command{2} = 'PING';
        }
        sleep 5;
        lock %command;
        foreach my $id (1..2)
        {
                if ($command{$id} eq 'PING')
                {
                        print "thread $id looks dead\n";
                }
                else
                {
                        print "thread $id looks alive\n";
                }
        }
        sleep 5;
}


$thread->join;

sub startThread
{
        my $id = shift;

        threads->self->create(\&pingThread,$id);
        print "Thread $id: started\n";
        while (1) 
        { 
                die "$id is dead\n" if (int(rand(20)) == 1);
                print "$id do stuff\n";
                sleep(1);
        }
}

sub pingThread
{
        my $id = shift;
        
        while (1)
        {
                {
                        lock %command;
                        if ($command{$id} eq 'PING')
                        {
                                $command{$id} = 'PONG';
                                print "$id: I've been pinged!\n";
                        }
                }
                sleep (1);
        }
}
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to