I'd like to make a little utility that can be scripted using Perl (of course). However, I'd really like to let the user abort the program with a "Cancel" button (in case, say, they manage to construct an infinite loop). Under Linux, I can interrupt the interpreter with a well-placed SIGINT, which is convenient because a carefully crafted $SIG{INT} in conjunction with caller() can give a complete backtrace (so the user can easily track down where the problem is).
// see perldoc perlembed (http://www.perldoc.com/perl5.8.0/pod/perlemb +ed.html) #include <EXTERN.h> #include <perl.h> #include <pthread.h> // pthreads: http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.htm +l #include <pthread.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> 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.htm +l // 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); }
It took a lot of effort to make this compile under Debian, so for those less fortunate (or patient) than I, here's the output:
creating new thread... thread 1... thread 2... thread 3... thread 4... thread 5... interrupted at fake.pl line 1.
Now, my question is this: How would I accomplish a similar effect under Win32?
--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

In reply to Interrupting Perl from another thread under Win32 by Stevie-O

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.