develooper Front page | perl.perl5.porters | Postings from July 2002

Thread::Use - use a module inside a thread only

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
July 29, 2002 13:55
Subject:
Thread::Use - use a module inside a thread only
Message ID:
4.2.0.58.20020729225004.0348a670@mickey.dijkmat.nl
Another one in the category "dirty, but maybe useful".

Hope this will make lean (memory wise) threaded programming a little easier.

Again, comments are welcome...  ;-)


Liz
=====================================================================
The uploaded file

     Thread-Use-0.01.tar.gz

has entered CPAN as

   file: $CPAN/authors/id/E/EL/ELIZABETH/Thread-Use-0.01.tar.gz
   size: 2972 bytes
    md5: 8179f44ff8a2ed31b8052d84cb250369

=head1 NAME

Thread::Use - use a module inside a thread only

=head1 SYNOPSIS

     use Thread::Use;

     threads->new( sub {
      useit Module;
      useit Module qw(parameters);

      noit Module;
      noit Module qw(parameters);
     } );

=head1 DESCRIPTION

When you are programming threaded applications and are interested in saving
memory by selectively loading modules inside threads only, you will find that
even if you C<use> a module inside a thread, it is in fact available to B<all>
threads, including the main thread.

This is caused by the fact that C<use> is executed at compile time.  At which
time Perl doesn't know anything about threads yet.

However, some modules, specifically the ones that are (partly) implemented
in XS, do not (yet) survive the cloning process that is involved with creating
threads.  So you can only use these modules inside threads only.  But if you
C<use> a module, it will be read in at compile time.

Of course, a C<use> is nothing more than a C<require> followed by a call
to the "import" class routine (if available).  But that doesn't feel natural
to do.  So this module allows you to use the C<useit> (for B<use> B<i>n
B<t>hreads command to indicate that a module should only be used inside
a thread.

For example: suppose you only need the C<PerlIO::gzip> module inside a thread:

  use Thread::Use;  # can be anywhere in your program

  threads->new( \&zipfile,filename,contents ); # start the thread

  sub zipfile {
    useit PerlIO::gzip;  # only use inside this thread
    open( my $out,'>:gzip', $_[0] ) or die "$_[0]: $!";
    print $out $_[1];
    close( $out );
  }

For completeness, it is also possible to pass any parameters as you would
with the C<use> command.  So:

  sub storable {
    useit Storable qw(freeze); # export "freeze" to namespace of thread
    my $frozen = freeze( \@_ );
  }

or to use the opposite C<no> equivalent;

  sub warnings {
    useit warnings "all";
    # special code
    noit warnings "all";
  }

=head1 CAVEATS

This modules is still experimental and subject to change.  At the current
stage it is more a proof of concept than anything else.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About