develooper Front page | perl.perl5.porters | Postings from January 2003

Re: thread safe

Thread Previous
Benjamin Goldberg
January 31, 2003 12:06
Re: thread safe
Message ID: wrote:
> I am really new here so bear with me, please.
> Has anyone looked at making perl thread safe to embed?  I would like
> to be able to launch an interpreter per thread from a C program
> (without context switching).  Has anyone researched this?

Perl already has support for threads.

The threading model is called "ithreads", or interpreter-threads, where
there is one interpreter per thread.  The pointer to the interpreter
isn't a global variable, it's passed around as a function argument, so
there's no need to perform explicit context switching.

The perl interface to thread creation is the "threads" module's "create"

   my $thr = threads->create( \&coderef, @args );

The new interpreter is effectively a clone of the one that created it
(except for the calling stack, which is reset so that \&coderef is the
top-most level).

However, AFAIK, there is no interface to create these interpreter
threads from C directly... You have to use the threads->create method. 
That is, you *cannot* create a thread with pthread_create or whatever,
then create an interpreter for it -- you *must* let perl do the thread
creation for you (this is because the new interpreter is created first,
*then* the new thread for it).

Hmm... well actually... didn't mod_perl use ithreads before a perl-level
interface for it was introduced in 5.7.3?  If so, then you could go
figure out how it was done there, and do that.

Well anyway, if you want to learn more about perl's threads, install
perl 5.8, and then read perldoc perlthrtut and perldoc threads.

$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About