Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem when embedding 2 Perl interpreters in C++

Reply
Thread Tools

Problem when embedding 2 Perl interpreters in C++

 
 
ritesh
Guest
Posts: n/a
 
      07-11-2006
Hi,

I'm facing a problem when embedding two perl interpreters in my C++
code.

My code has 2 threads - the GUI and the Core thread. The core thread
initializes a Perl interpreter and all works fine here. Both the
threads communicate via Events.

In a particular scenario the GUI thread, invokes a function which
further invokes a perl subroutine using the "perl_call_argv" call.
Since the GUI thread dosen't have a perl interpreter running on it
there are 2 options I have -

1. Use the PERL_SET_CONTEXT macro within the function that the GUI
thread calls, and then invoke the perl subroutine using the
"perl_call_argv" call. -- This works fine.

However I'm not sure if two threads should be sharing the same perl
interpreter. The Core thread might also make some calls to
"perl_call_argv" while the GUI thread is working. Would the
interpreter be intelligent enough to handle 2 threads? I found a
negative answer to this question at this link -
modperlbookDOTorg/html/ch24_03DOThtml
The text explicity states --> "This of course requires that each Perl
interpreter instance is accessed
by only one thread at any given time."

2. In second scenario, I create a new perl interpreter within the
function the GUI thread calls. Creation and destruction of the
interpreter happens within this function, since the interpreter is not
required by the GUI thread after the function returns. Here is the
function -

void function_for_gui_thread(char * perlFnToCall)
{
PL_perl_destruct_level = 1; /* keep setting this value to 1
before construction and destruction of the interperator for gui thread
*/

PerlInterpreter * guiPerlInterp = 0; /* this is the interp i
create within the function for GUI thread */
perlInterp = perl_alloc();
PERL_SET_CONTEXT(guiPerlInterp);
perl_construct(guiPerlInterp);
PL_perl_destruct_level = 1;
perl_run(guiPerlInterp);


char ** perl_args = (char**)sgMalloc(2*sizeof(char*));
perl_args[0] = "";
perl_args[1] = 0;
perl_call_argv(perlFnToCall, G_DISCARD, perl_args); /* for
simplicity I've added the perlFnToCall variable in the function
parameters, however it is determined in this function at runtime */
sgFree(perl_args);


PL_perl_destruct_level = 1;
perl_destruct(guiPerlInterp);
PL_perl_destruct_level = 1;
perl_free(guiPerlInterp);
PERL_SET_CONTEXT(corePerlInterpreter); /* corePerlInterpreter
is the interp initialized by the core thread */
}

Note that I set the context of the interpretor to the new guiPerlInterp
before I start using it. When exiting the function I set the context
back to corePerlInterpreter.

First question that comes to my mind is - Do I really need to set the
context back to corePerlInterpreter for the GUI thread, since it
dosen't need a perl interpreter after this function call?

Second when I run the program, the perl subroutine is invoked (i've
verified this using print commands), then within the perl subroutine
certain subroutines are invoked which use Perl XS routines. When the
control reaches the first such subroutine I get an error like this -

Usage : getReport(reportName)

and the program exits. The "getReport(reportName)" is the XS function
that the perl function invokes.

------------------------------------

Could you please help me out with this. The program works fine when I
use the first scenario. However I would like to use the second
scenario.

Thanks,
ritesh

 
Reply With Quote
 
 
 
 
ritesh
Guest
Posts: n/a
 
      07-11-2006
One point that I missed out -

Right now I'm not sure if the perl I have on my system is compiled
using the options -
-Dusethreads -Duseithreads

Could this be the reason that the second scenario is giving these
"Usage : ..." errors?

Thanks,
ritesh

 
Reply With Quote
 
 
 
 
Stephan Titard
Guest
Posts: n/a
 
      07-12-2006
ritesh escribió:
> Hi,
>
> I'm facing a problem when embedding two perl interpreters in my C++
> code.
>
> My code has 2 threads - the GUI and the Core thread. The core thread
> initializes a Perl interpreter and all works fine here. Both the
> threads communicate via Events.
>
> In a particular scenario the GUI thread, invokes a function which
> further invokes a perl subroutine using the "perl_call_argv" call.
> Since the GUI thread dosen't have a perl interpreter running on it
> there are 2 options I have -
>
> 1. Use the PERL_SET_CONTEXT macro within the function that the GUI
> thread calls, and then invoke the perl subroutine using the
> "perl_call_argv" call. -- This works fine.
>
> However I'm not sure if two threads should be sharing the same perl
> interpreter.

I believe not.

The Core thread might also make some calls to
> "perl_call_argv" while the GUI thread is working. Would the
> interpreter be intelligent enough to handle 2 threads? I found a
> negative answer to this question at this link -
> modperlbookDOTorg/html/ch24_03DOThtml
> The text explicity states --> "This of course requires that each Perl
> interpreter instance is accessed
> by only one thread at any given time."

that makes sense to me
>
> 2. In second scenario, I create a new perl interpreter within the
> function the GUI thread calls. Creation and destruction of the
> interpreter happens within this function, since the interpreter is not
> required by the GUI thread after the function returns. Here is the
> function -
>
> void function_for_gui_thread(char * perlFnToCall)
> {
> PL_perl_destruct_level = 1; /* keep setting this value to 1
> before construction and destruction of the interperator for gui thread
> */
>
> PerlInterpreter * guiPerlInterp = 0; /* this is the interp i
> create within the function for GUI thread */
> perlInterp = perl_alloc();
> PERL_SET_CONTEXT(guiPerlInterp);
> perl_construct(guiPerlInterp);
> PL_perl_destruct_level = 1;
> perl_run(guiPerlInterp);
>
>
> char ** perl_args = (char**)sgMalloc(2*sizeof(char*));
> perl_args[0] = "";
> perl_args[1] = 0;
> perl_call_argv(perlFnToCall, G_DISCARD, perl_args); /* for
> simplicity I've added the perlFnToCall variable in the function
> parameters, however it is determined in this function at runtime */
> sgFree(perl_args);
>
>
> PL_perl_destruct_level = 1;
> perl_destruct(guiPerlInterp);
> PL_perl_destruct_level = 1;
> perl_free(guiPerlInterp);
> PERL_SET_CONTEXT(corePerlInterpreter); /* corePerlInterpreter
> is the interp initialized by the core thread */
> }
>
> Note that I set the context of the interpretor to the new guiPerlInterp
> before I start using it. When exiting the function I set the context
> back to corePerlInterpreter.
>
> First question that comes to my mind is - Do I really need to set the
> context back to corePerlInterpreter for the GUI thread, since it
> dosen't need a perl interpreter after this function call?
>
> Second when I run the program, the perl subroutine is invoked (i've
> verified this using print commands), then within the perl subroutine
> certain subroutines are invoked which use Perl XS routines. When the
> control reaches the first such subroutine I get an error like this -
>
> Usage : getReport(reportName)
>
> and the program exits. The "getReport(reportName)" is the XS function
> that the perl function invokes.
>
> ------------------------------------
>
> Could you please help me out with this. The program works fine when I
> use the first scenario. However I would like to use the second
> scenario.
>
> Thanks,
> ritesh
>

if you don'use dynamic loading (dlopen and such) you already pay the
price of loading the perl library...
in that case maybe you could start the perl interpreter from the main
program (thread) and mutex the calls to it from any thread (GUI or other...)

sorry I cannot help more on this, I haven't hooked perl into C++
maybe you could post this on p5p, if nobody else answers

hth
--stephan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with help() in python/ipython interpreters Casey Python 1 05-12-2008 09:24 AM
Embedding: many interpreters OR one interpreter with many thread states ? adsheehan@eircom.net Python 3 06-10-2005 03:18 AM
Correct way to handle independent interpreters when embedding in asingle-threaded C++ app Craig Ringer Python 1 11-18-2004 11:35 PM
Specifying multiple perl interpreters for a script Yash Perl Misc 3 07-14-2004 04:16 PM
Experts on embedding Perl in C wanted: Weird problem on RH7.3/Perl 5.6.1 David F. Skoll Perl 2 11-14-2003 05:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57