Sorry BrowserUK for being imprecise,
It's not so much a matter of imprecision as omission; but no matter. To the problem.
You haven't show what where $image in $image->setpixel() comes from. Its not GD (which has setPixel()) so I'm assuming it comes from Imager or Image::Magick or a similar package.
The first question is: are the C/C++ libraries that underly that module thread-safe.
To explain. If I run this short script that uses GD, it displays a 100x100 png with the left half red and the right half blue:
#! perl -slw use strict; use threads; use GD; sub rgb2n{ local $^W; unpack 'N', pack 'CCCC', 0, @_ } my $i = GD::Image->new( 100, 100, 1 ); # async{ $i->filledRectangle( 0,0, 50, 100, rgb2n( 255, 0, 0 ) ); # }->join; $i->filledRectangle( 51,0, 100, 100, rgb2n( 0,0, 255 ) ); open O, '>:raw', 'junk.png' or die $!; print O $i->png; close O; system 'junk.png';
However, If I uncomment the two commented lines, thus the two rectangles are drawn to the image by different threads, I get this when I run it:
C:\test>junk999 gd-png: fatal libpng error: No IDATs written into file gd-png error: setjmp returns error condition
Ie. The libgd that underlies GD isn't threadsafe; and whilst the drawing to the image in the two threads executes without errors; when it comes to writing the image out to a file, a fatal error occurs. This isn't a limitation of perl's threading, but the underlying library.
Let's explore if it makes any sense to write and image from multiple concurrent threads.
Let's assume that one thread draws horizontal lines across the image in red, whilst another thread draws vertical bars down the image in blue. What should the resultant image look like?
Like this? (You'll have to use your imagination here (#is red * is blue):
** ** ** ** ** ** ##**###**###**###**###**###**# ** ** ** ** ** ** ##**###**###**###**###**###**# ** ** ** ** ** ** ##**###**###**###**###**###**# ** ** ** ** ** ** ##**###**###**###**###**###**# ** ** ** ** ** **
Or like this?:
** ** ** ** ** ** ############################## ** ** ** ** ** ** ############################## ** ** ** ** ** ** ############################## ** ** ** ** ** ** ############################## ** ** ** ** ** **
Or some variation on this?:
** ** ** ** ** ** #######**###**###**###**###**# ** ** ** ** ** ** ############**###**###**###**# ** ** ** ** ** ** #################**###**###**# ** ** ** ** ** ** ######################**###**# ** ** ** ** ** **
Now you're probably thinking that as you're only using setPixel, and never writing to the same pixel twice, it doesn't matter what order things occur; but most graphics libraries retain a whole bunch of state between drawing calls -- eg. current clip region; current line pattern; current alpha setting; current .... -- and if two threads start modifying that internal state without using locking, then the internals of the image; and even the entire library can become corrupted.
That's what happened with my GD example.
Enough for one reply, more in the next.
In reply to Re^3: Threads From Hell #3: Missing Some Basic Prerequisites
by BrowserUk
in thread Threads From Hell #3: Missing Some Basic Prerequisites [Solved]
by karlgoethebier
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |