Pots::Thread - Perl ObjectThreads Thread Object
# 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();
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.
Pots::Thread
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.
Pots::MessageQueue
Pots::Message
Pots::Tutorial
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.
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.
Pots::Thread objects do not start by default when created. You must call "start()" on them so they start running.
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.
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.
This method, if implemented in your derived class, is called after the "run()" method.
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.
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.
This is a combination of "postmsg()" and "getmsg()". This method sends a message and waits for its reply.
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.
To install Pots::Thread, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pots::Thread
CPAN shell
perl -MCPAN -e shell install Pots::Thread
For more information on module installation, please visit the detailed CPAN module installation guide.