Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

But you're using a different graphics 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.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-03-29 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found