Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Interrupting Perl from another thread under Win32

by Stevie-O (Friar)
on Apr 20, 2004 at 23:37 UTC ( #346828=perlquestion: print w/replies, xml ) Need Help??

Stevie-O has asked for the wisdom of the Perl Monks concerning the following question:

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 ( +ed.html) #include <EXTERN.h> #include <perl.h> #include <pthread.h> // pthreads: +l #include <pthread.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> const char perlscript[] = "#line 1 \"\"\n" "$SIG{INT} = sub { die 'interrupted' };\n" "for (1..10) {\n" " print qq[thread $_...\\n];\n" " sleep(1);\n" "}\n"; // pthreads: +l // +/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 line 1.
Now, my question is this: How would I accomplish a similar effect under Win32?
$"=$,,$_=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

Replies are listed 'Best First'.
Re: Interrupting Perl from another thread under Win32
by Ryszard (Priest) on Apr 21, 2004 at 10:39 UTC
    Mmmmmm I dnt know if your problem lends its self to this, but, why not have the little utility listen on a socket for some kind of interupt signal.

    You could poll the socket periodically and take whatever activity you need?

      Unfortunately, if it's stuck in an infinite loop, it's probably not going to be listening for a 'break out' signal.
      $"=$,,$_=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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://346828]
Approved by kvale
Front-paged by matija
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-05-13 04:20 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (134 votes). Check out past polls.