// see perldoc perlembed (http://www.perldoc.com/perl5.8.0/pod/perlembed.html) #include #include #include // pthreads: http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html #include #include #include #include const char perlscript[] = "#line 1 \"fake.pl\"\n" "$SIG{INT} = sub { die 'interrupted' };\n" "for (1..10) {\n" " print qq[thread $_...\\n];\n" " sleep(1);\n" "}\n"; // pthreads: http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html // http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html void* thread(void *arg) { char *perlargs[] = { "", "-e0" }; static PerlInterpreter *my_perl; // I tried naming this 'perl' but PL_exit_flags was unwilling // taken nearly verbatim from perlembed my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, 2, perlargs, (char**)NULL); perl_run(my_perl); eval_pv(perlscript, TRUE); perl_destruct(my_perl); perl_free(my_perl); return NULL; } int main(int argc, char *argv[]) { pthread_t tid; printf("creating new thread...\n"); pthread_create(&tid, NULL, thread, NULL); sleep(5); pthread_kill(tid, SIGINT); pthread_join(tid, NULL); } #### creating new thread... thread 1... thread 2... thread 3... thread 4... thread 5... interrupted at fake.pl line 1. #### $"=$,,$_=q>|\p4<6 8p .q>.<4-KI;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/