Thanks BrowserUk & Corion for your help. I created a very basic example here in order to demonstrate the problem. Just for clarity, there are only 2 threads in here. Main thread - calling MyRegister() and MyDeregister(). A callbackworker thread is spawned inside the library to fire callbacks. Random sleep is introduced in both threads to simulate scenario when both might wake up next to each other and run into race conditions somewhere inside the interpreter code. The global pointer is just as intended. I am not concerned about callback registration being called twice.

BrowserUk, not sure if I understood you correctly, I checked for the value returned by Perl_get_context() it returns same value throughout the execution of the program. Therefore I'm in an understanding that there is only one perl interpreter here and only one context. The latest link you provided is close to the issue I'm trying to solve will take a good look and let you know.

Corion, Async::Interrupt() as you suggested looks to be a good solution here. I believe the issue happens only when perl interpreter is forced to do 2 things from 2 threads at race conditions. for ex. "print" from main thread and context switches to "print" from callback subroutine. I suspect the call_pv() function in wrap_connect_cback_handler() function is NOT reentrant in nature (some others may not be as well.)

Meanwhile, I tried Win32::Event to wait for callback in the main thread. The event is Set at the end of the callback subroutine. It worked fantastic. This approach has more overhead than Async::Interrupt() for an event driven scenario albeit simpler.

Thanks.

In reply to Re^2: Perl interpreter throws strange errors for event-driven callback processing under race conditions by Cotton4Lunch
in thread Perl interpreter throws strange errors for event-driven callback processing under race conditions by Cotton4Lunch

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.