(none) imager-devel
/ help / lists / applications / search /

Re: Imager and threads

From: Tony Cook (
Date: Fri 21 Sep 2007 - 18:01:25 GMT

  • Next message: Ilya Dogolazky: "Re: Imager and threads"

    On Fri, Sep 21, 2007 at 04:16:20PM +0200, Ilya Dogolazky wrote:
    > Hi!
    > I have a problem with Imager in a multi-threaded Perl script. I have
    > written a very simple script (without Imager) and it works well. But
    > if I uncomment the line "use Imager" (so, I am only loading Imager.pm,
    > do not ever use it!) the script either terminates by SIGSEGV or does
    > not terminate and runs for ever and ever...
    > My perl version is 5.8.4, Imager of the version 0.44 is installed on
    > my computer (running Debian 3.1 GNU/Linux 2.6.16mib). The same problem
    > occurs with the version 0.60 of Imager.pm

    Imager doesn't support threads.

    The problem you're seeing occurs because of the following sequence of

    1) on load, Imager creates a small number of Imager::Color objects,
    which are XS T_PTROBJ objects

    2) threads::create() copies the SV and creates new references to the
    original Imager::Color objects (which are pointers to the same
    underlying C structure)

    3) a thread ends, destroying the cloned Imager::Color object, freeing
    the C structure

    4) another thread ends, destroying another clones Imager::Color
    object, freeing the same structure again, trashing the memory arena.

    You can avoid this specific problem in perl 5.8.7 and later by

    sub Imager::Color::CLONE_SKIP { 1 }

    but this won't make Imager threadsafe.

    Possibly future versions of Imager will support threads at the same
    level as DBI - ie. you need to create all objects in the thread
    involved, but even that will require some significant work.