Recently, I noticed a blurb in perlguts which said all I had to do was
do PERL_SET_CONTEXT() and everything would be happy.  (This perlguts
entry seems unclear as to whether this will work for concurrent calls,
or just for the occasional call on its own.)  So I tried it.  And
everything does work, if I put a lock around the whole thing.  It
crashes horribly if I call into it multiple times concurrently.

So, I did a little more research.  It looks like I have to call
perl_clone(), but that crashes when I call into it concurrently, too.
And this time I have an additional problem: none of the arguments get
passed down to the callback sub!

Ok - this works for me (both Win32 and Linux).

As early as you can perform a clone on the main thread, then save this cloned Perl for later use. When you start spawning your worker threads, you need to clone from the saved clone. You will also need a mutex around your cloning logic. Some rough code:


PerlInterpreter* ClonePerl() {
 //lock the mutex before we do anything
 PerlInterpreter *NewPerl;
 PerlInterpreter *my_perl;
 pthread_mutex_lock(&CloneMutex);
 PERL_SET_CONTEXT(savedPerlClone);
 NewPerl = perl_clone(savedPerlClone,XXX); //XXX your flags
 //you would check if NewPerl is valid...
 //unlock the mutex
 pthread_mutex_unlock(&CloneMutex);
 //return the new perl
 return NewPerl;
}


void worker() {
 //worker thread init
 PerlInterpreter* my_perl;
 my_perl = ClonePerl();
 //each worker now has it's own perl, and you can do what you want to it:)
}

Cheers,

jez.


Reply via email to