You don't want to call into perl from two different threads at the same time. That will cause the crashing you're seeing.

One approach would be to have an event queue that you pull off of from Perl, and push onto from the C callback.

So instead of:

void my_callback(void *data) { call_into_perl_event_handler(data); }
do something like:
// XS: void my_callback(void *data) { pthread_mutex_lock(&event_queue_lock); push_onto_event_queue(data); pthread_mutex_unlock(&event_queue_lock); pthread_cond_broadcast(&event_queue_not_empty); } void get_next_event() PPCODE: pthread_mutex_lock(&event_queue_lock); while (event_queue_empty()) pthread_cond_wait(&event_queue_not_empty, &event_queue_loc +k); void *data = pull_off_event_queue(); pthread_mutex_unlock(&event_queue_lock); EXTEND(SP, 1); PUSHs(sv_2mortal(newSVpvn(data, ...))); # Perl: while (my $event = get_next_event()) { event_handler($event); }
Hope that helps.

In reply to Re: Callback from threaded shared library by PreferredUserName
in thread Callback from threaded shared library 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.