Perl’s documentation says:
Since Perl 5.8, thread programming has been available using a model called interpreter threads which provides a new Perl interpreter for each thread
Using ps -Lm <pid> with the program below I can see that threads run in parallel, i.e., they are being run at the same time in different cores. But even when there are 4 threads (3 and the main) ps aux shows only one Perl process.
- Does this mean that there is a whole Perl interpreter on each thread?
- Are Perl threads mapped to system threads?
- If 2 is true, how is possible to have multiple Perl interpreters within a single process?
use threads;
$thr = threads->new(\&sub1);
$thr2 = threads->new(\&sub1);
$thr3 = threads->new(\&sub1);
sub sub1 {
$i = 0;
while(true){
$i = int(rand(10)) + $i;
}
}
$thr->join;
“Perl interpreter” refers to the environment in which the Perl code executes. From a user’s perspective, that’s mostly the symbol table and the globals therein, but it also includes a slew of internal variables (e.g. those used during parsing, the current op, etc).
Yes, there’s a Perl interpreter for each thread.
Yes, Perl threads are system threads.
Think of “Perl interpreter” as a class of which you can make any number of instances.* Perl refers to this as Multiplicity. See perlembed for how to embed a Perl interpreter in your application.
* — Requires the use of
-Dusemulitplicitywhen building Perl, which is implied by-Dusethreads, which is how thread support is added to Perl. Otherwise, a whole bunch of globals are used instead of a “class”.