As with most of these 'non-threadsafe' problems, it is the near useless process of implicit cloning, that is at fault. If you avoid it, and manually load the code into the thread where it is used, it works fine:

#! perl -slw use strict; use threads; sub t { require Math::Pari; eval { use Math::BigInt; }; my $mbi = Math::BigInt->new( $_[ 0 ] ); $mbi /= 11; return $mbi; } print threads->create( \&t, '123' x 11 )->join; __END__ c:\test>t-pari 11193011193011193011193011193011

Using Math::BigInt without Math::Pari in multiple threads this way also works:

#! perl -slw use strict; use threads; sub t { # require Math::Pari; eval { use Math::BigInt; }; my $mbi = Math::BigInt->new( $_[ 0 ] ); $mbi /= 11; return $mbi; } print threads->create( \&t, '123' x 11 )->join; print threads->create( \&t, '123' x 21 )->join; __END__ c:\test>t-pari 11193011193011193011193011193011 11193011193011193011193011193011193011193011193011193011193011

But with Math::Pari it does not. This code hangs:

#! perl -slw use strict; use threads; sub t { require Math::Pari; eval { use Math::BigInt; }; my $mbi = Math::BigInt->new( $_[ 0 ] ); $mbi /= 11; return $mbi; } print threads->create( \&t, '123' x 11 )->join; print threads->create( \&t, '123' x 21 )->join; __END__ c:\test>t-pari 11193011193011193011193011193011 Terminating on signal SIGINT(2)

Which seems to put the problem within Math::Pari. The following from the Math Pari POD may give some indication to the cause:

Legacy implementations of dynalinking require the code of DLL to be compiled to be "position independent" code (PIC). This slows down the execution, while allowing sharing the loaded copy of the DLL between different processes. On contemeporary architectures the same effect is allowed without the position-independent hack.

Currently, PARI assembler files are not position-independent. When compiled for the dynamic linking on legacy systems, this creates a DLL which cannot be shared between processes. Some legacy systems are reported to recognize this situation, and load the DLL as a non-shared module. However, there may be systems (are there?) on which this can cause some "problems".

Summary: if the dynaloading on your system requires some kind of C<-fPIC> flag, using "assembler" compiles (anything but C<machine=none>) *may* force you to do a static build (i.e., creation of a custom Perl executable with

perl Makefile.PL static make perl make test_static
).

I don't have Math::GMP, so I haven't checked whether any of hthis translates to that module.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Writing threadsafe perl extensions by BrowserUk
in thread Writing threadsafe perl extensions by syphilis

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.