The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Pots::Thread - Perl ObjectThreads Thread Object

SYNOPSIS

    # Simple useless example
    use Pots::Thread;
    my $th = Pots::Thread->new();
    $th->start();
    ...
    $th->stop();

    # Simple less useless example
    package MyThread;
    use base qw(Pots::Thread);

    sub new {
        my $class = shift;
        my %p = @_;

        my $self = $class->SUPER::new(%p);

        return $self;
    }

    sub initialize {
        my $self = shift;
        my %p = @_;

        $self->SUPER::initialize(%p);
        $self->{'my_option} = $p{'my_option'} if ($p{'my_option'});

        return 1;
    }

    sub pre_run {
        my $self = shift;

        ...
    }

    sub run {
        my $self = shift;
        my $quit = 0;
        my $msg;

        while (!$quit) {
            $msg = $self->getmsg();

            for ($msg->type()) {
                if (/quit/) {
                    $quit = 1;
                } else {
                }
            }
        }
    }

    sub post_run {
        my $self = shift;

        ...
    }

    package main;

    my $th = MyThread->new(
        my_option => 'foo',
        args => qw(arg1 arg2 arg3)
    );

    $th->start();
    ...
    $th->stop();

DESCRIPTION

Pots::Thread allows you to use Perl 5.8 ithreads in an object-oriented way. It is not very usefull on its own but rather as a base class.

It has a built-in message queue implemented using a Pots::MessageQueue object. Using that queue, you can send Pots::Message objects to the thread. See Pots::Tutorial for examples.

METHODS

new ([ARGS])

This will create a new Pots::Thread object. You can pass arguments, as key-value pairs.

The following keys are automatically handled:

  detach     create a detached thread (default: 1)
  args       parameter(s) to be passed to the thread's "pre_run", "run"
             and "post_run" methods.
initialize ()

This method is called to allow you to initialize your object before the thread is created. You should call the parent's initialize and return a true value, otherwise object creation will fail.

The second argument is the parameter hash used when "new()" was called.

start ()

Pots::Thread objects do not start by default when created. You must call "start()" on them so they start running.

pre_run ()

This method, if implemented in your derived class, is called just after the thread is created and runs in the new thread context. It is passed the "args" parameter(s) specified when you called "new()".

It should return a true value to indicate success, or the thread will stop.

run ()

This method, if implemented in your derived class, is called after the thread is started (with "start()") if the "pre_run()" method has succeeded.

This is the typical place to do something useful.

Its return value will be returned by the "stop()" method.

post_run ()

This method, if implemented in your derived class, is called after the "run()" method.

postmsg ($msg)

Allows you to send a Pots::Message to the running thread. If called in the thread context, it will send a message to the parent thread. After posting the message, the method returns immediately. See Pots::Message for further information.

getmsg ()

Allows you to retrieve messages sent by the thread (e.g.: in response to a message sent with "postmsg()"). If called in the thread context, it will retrieve messages sent by the parent thread.

Returns a Pots::Message object, see Pots::Message for further information.

sendmsg ()

This is a combination of "postmsg()" and "getmsg()". This method sends a message and waits for its reply.

AUTHOR and COPYRIGHT

Remy Chibois <rchibois at free.fr>

Copyright (c) 2004 Remy Chibois. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.