[% setvar title MT-Safe Autovariables in perl 5.005 Threading %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


MT-Safe Autovariables in perl 5.005 Threading


  Maintainer: Michael Maraist <maraist@udel.edu>
  Date: 25 Sep 2000
  Mailing List: perl6-internals@perl.org
  Number: 293
  Version: 1
  Status: Developing


There are currently two threading models in Perl. The first was 5.005s true-threads. This obviously caused problems for numerous modules (and programs alike). It was later replaced by the "virtual" process, who's main benifit was to provide efficient forks through the use of spawning a perl-thread and seperating environment spaces..

Perl5.005's Threading may go away, or it may continue in an experimental stage for a while. At the very least, perl's threading provides an easy way to test CIS OS theory. This RFC attempts to identify at least one existing implementation problem with 5.005 threads, that of auto-variables. To my understanding, there is only one copy of each auto-variable, and thus, multiple threads would have race conditions for them.


Make auto-variables in a threading environment "thread-specific". The current implementation of:

 for my $i ( 0 .. 100000 ) { do something }

causes core dumps when there are race conditions. It is my guess that the problem is because this produces a lazy iteration which unfortunately makes use of an auto-variable other than $i (above). It is these sort of auto-vars (especially in light of lazy operators), that can cause race-conditions.

Normally you would provide locks to protect such code, but it is not obvious to the developer that a simple for-loop using a lexical variable would even have a race condition. Beyond that, it might be possible that the generation of such auto-vars might not be thread-safe.


For all auto-generated non-lexical meta-data, either apply a lock around usage, or make heavy use of thread-specific keys/variables.


RFC 185: Thread Programming Model

perldoc Thread: 5.005 threading

RFC 178: Lightweight Threads

RFC 1: Implementation of Threads in Perl

man perlop: foreach lazy iteration of 1..10

RFC 24: Data types: Semi-finite (lazy) lists

RFC 123: Builtin: lazy

RFC 128: Subroutines: Extend subroutine contexts to include name parameters and lazy arguments

RFC 285: Lazy Input