Hi, monks!
I have error in code when I mix XS(with callbacks) and threads. Simple code to demonstrate (it works fine without threads):
Callback.xs
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" CV* callback_ref; int runCallback(const char* param) { int count; int answer; dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(param, 0))); PUTBACK; count = call_sv((SV*)callback_ref, G_SCALAR); SPAGAIN; if (count != 1) croak("Uncknown return value format. Returned %d params instid 1", + count); answer = POPi; FREETMPS; LEAVE; return answer; } MODULE = Callback PACKAGE = Callback void registCallback(cb) CV* cb; PROTOTYPE: $ PPCODE: callback_ref = cb; void runCallback(param) const char* param; PROTOTYPE: $ PPCODE: XPUSHs(sv_2mortal( newSViv(runCallback(param)) ));

Callback.pm
package Callback; use 5.010000; use threads; use threads::shared; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); our $VERSION = '0.01'; require XSLoader; XSLoader::load('Callback', $VERSION); sub new { my $invocant = shift; my $callback_ref = shift; $invocant = ref $invocant if ref $invocant; registCallback($callback_ref); my $self = &share({}); return bless $self, $invocant; } sub run { my $self = shift; my $param = shift; runCallback($param); } 1;

test.pl
#!/usr/bin/env perl use strict; use lib qw(./Callback/build/lib/perl5/i386-linux-thread-multi/); use threads; use threads::shared; use Data::Dumper; use Callback; use constant MESSAGE => "Hellow world!!!\n"; my $obj = new Callback (\&printHellow); my $thread = threads->create(sub {$obj->run(&MESSAGE)}) or exit(1); $thread->join(); sub printHellow { my $message = shift; print "MESSAGE: $message\n"; }

OUTPUT:
MESSAGE:
panic: free from wrong pool during global destruction.

In reply to xs, threads and panic by Anonymous Monk

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.