#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_867652', CLEAN_AFTER_BUILD => 0; PerlInterpreter *saved1; SV *callback1; PerlInterpreter *saved2; SV *callback2; VOID CALLBACK cbProc1( DWORD time ) { //this always returns "0" PerlInterpreter *temp = Perl_get_context(); printf( "C-Callback1: %u\n", time ); //my code has a dTHX; here. Without it, I get errors that my_perl is undefined PERL_SET_CONTEXT( saved1 ); { dSP; ENTER; SAVETMPS; PUSHMARK( SP ); XPUSHs( sv_2mortal( newSVuv( (UV)time ) ) ); PUTBACK; call_sv( callback, G_DISCARD ); FREETMPS; LEAVE; } PERL_SET_CONTEXT( temp ); return; } VOID CALLBACK cbProc2( DWORD time ) { //this always returns "0" PerlInterpreter *temp = Perl_get_context(); printf( "C-Callback2: %u\n", time ); //my code has a dTHX; here. Without it, I get errors that my_perl is undefined PERL_SET_CONTEXT( saved2 ); { dSP; ENTER; SAVETMPS; PUSHMARK( SP ); XPUSHs( sv_2mortal( newSVuv( (UV)time ) ) ); PUTBACK; //the error msg shows up after calling this call_sv( callback, G_DISCARD ); FREETMPS; LEAVE; } PERL_SET_CONTEXT( temp ); return; } void __cdecl thread1( VOID *arg ) { while( Sleep( 5000 ), 1 ) { printf( "Here in thread1\n" ); cbProc1( GetTickCount() ); } } void __cdecl thread2( VOID *arg ) { while( Sleep( 5000 ), 1 ) { printf( "Here in thread2\n" ); cbProc2( GetTickCount() ); } } void setCallback1( SV* cv ) { //my code did not have the dTHX here, but adding it made no difference (i.e saved1 the same). dTHX; //saved1 and saved2 are always the same too. saved1 = Perl_get_context(); callback1 = cv; // Start callback timer thread _beginthread( &thread1, 0, NULL ); return; } void setCallback2( SV* cv ) { //my code did not have the dTHX here, but adding it made no difference (i.e saved2 is the same). dTHX; saved2 = Perl_get_context(); callback2 = cv; // Start callback timer thread _beginthread( &thread2, 0, NULL ); return; } END_C $|++; my cb1_cnt = 0; sub callback1 { print "Timer value in callback1 is: $_[0]"; cb1_cnt++; return; } my cb2_cnt = 0; sub callback2 { print "Timer value in callback2 is: $_[0]"; cb2_cnt++; return; } setCallback1( 'callback1' ); while (cb1_cnt != 4) { sleep(1); } setCallback2('callback2'); while (cb2_cnt != 4) { #the error msg will point to this line. Not sure if its just a #coincidence that it points here.. sleep(1); } sleep (15); __END__