Perl Thread Safe Modules Perl Thread Safe Modules multithreading multithreading

Perl Thread Safe Modules


Generally speaking, core and high-visibility modules are thread-safe unless their documentation says otherwise.

That said, there are a few missteps in your post:

  1. share($CSV)
    This clears $CSV (a blessed hashref), just as documented in threads. Generally, you want to share() complex objects prior to initialization or, perhaps in this case, share() some dumb $lock variable between threads.
    Since $CSV holds state for the underlying XS, this might lead to undefined behavior.

    But this isn't your segfault.

  2. threads->create(\&sayHello('1'));
    You are mistakenly invoking sayHello(1) in the main thread and passing a reference to its return value to threads->create() as a (bogus) start routine.You meant to say:

    threads->create(\&sayHello, '1');

    But this isn't your segfault.

    (EDIT Just to clarify -- a bad start routine here doesn't risk a SEGV in any case. threads::create properly complains if an unrecognized subroutine name or non-CODE ref is passed in. In your case, however, you are segfaulting too quickly to reach this error handling.)

  3. Encodings are not thread-safe.
    Again as documented in encodings, the encoding module is not thread-safe.Here's the smallest possible code I could get to reproduce your symptoms:

    use threads;open my $OUTPUT , ">:encoding(utf8)", "/dev/null" or die $!;threads->create( sub {} )->join;

    That's perl 5.12.1 with threads-1.77 on i686-linux-thread-multi, if you're interested. Drop the "utf8" magic, and it works just fine.

    This is your segfault