in reply to Callback from threaded shared library

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.