zimmerman80 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to embed perl code within C. All is good until I want to do multiple threads- bad things then occur. I'm allocating a PerlIntrepreter for each thread by using perl_alloc() and also setting the context before each command. Here's my code:

PL_perl_destruct_level = 1;

my_perl = perl_alloc();
PERL_SET_CONTEXT(my_perl);
perl_construct(my_perl);
perl_parse(my_perl, NULL, 3, Embedding, (char **)NULL);
perl_run(my_perl);

On a different, but possibly related note, perl_alloc seems to return a null pointer, although this does not affect the single-threaded process. All help is appreciated.

Replies are listed 'Best First'.
Re: threaded C embed
by stephen (Priest) on May 18, 2001 at 03:30 UTC
    I'll admit that I haven't done this, but the perlman:perlembed manpage says:
    Now suppose we have more than one interpreter instance running at the same time. This is feasible, but only if you used the -DMULTIPLICITY flag when building Perl. By default, that sets PL_perl_destruct_level to 1.

    So the first thing that I'd check is that you'd built Perl with -DMULTIPLICITY. Try not to think of Michael Keaton movies. :)

    Even there, you may be out of luck. IIRC, Perl is not entirely multithread-safe. According to the Perl5-porters list, Perl 5 uses things like setjmp and longjmp, which are not multi-thread safe. You may need to share a single interpreter among the multiple threads, or otherwise prevent multiple interpreters from getting called in the same instant.

    Best of luck!

    Update Added "out of luck" paragraph after further research.

    stephen