Basically, you cannot call perl code across threads. If you create an interpreter in the context of your main thread, call into it and retrieve a code reference, either directly, or indirectly in the form of method addresses attached to a returned object, attempting to call that/those address from a separate thread is going to crash.

To allow multiple perl threads to run, a separate interpreter is started in each thread, and all the context they require to run is stored in thread-local storage. If you pass a callback generated in one interpreter (thread) across thread boundaries within your C code and then attempt to call it, the thread relative context will be missing and a crash is inevitable.

Even if you started a second interpreter in the second thread, a callback created from within the first thread/interpreter will not work if passed to the second interpreter.

The best option would be to start your interpreter in it's own thread and have the perl code in that thread go into an endless loop blocking on a read from a queue.

Whenever your other (C) threads need something done by the perl thread, the post a token onto the queue that tells the perl code what is required. The C thread then blocks reading the queue until the perl code push the required results back onto the queue. You'll need to have the request tokens passed in carry an identifier (threadid) of the "calling thread" so that it can attach the same identifier to the results it posts for that thread. The C code reading the queue will need to discriminate between replies intended for it, and those intended for other calling threads, and leave any not intended for it on the queue and contniue blocking.

This is the message loop model used by gui's like tk and others.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

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