Hey everybody,

I'm facing some difficulties when I'm trying to implement a thread in my program. I don't know if I am thinking wrong about the usage of threads or if I'm facing some other problems?

First I'll try to explain the basic of the program which I'm trying to implement thread in.

I'm working in a Unix environment and I'm using Tk and Imager modules

The program basicly reads in an image and then draws it pixels by pixels on a canvas. Because it is taking some time to draw on the canvas if it is a big image, I want to add a progressbar while drawing the image.

Butt when I add the progress bar in the same sub routine as the drawing subroutine, it becomes even more slower. So I thought to use threads, by doing this I can work parallel on drawing on the progress bar at the same time. Right?

Because I never used threads before, I have read some articles about it at the perlthrtut pages in the hope of learning how threads works.

After reading this all, I managed to write this piece of code:

# Calculate the pixels and divide by 100 if ($first_start == "1"){ $tot_pix = $x * $y; $one_hunderd = $tot_pix / 100; } # Loop for drawing all for ($width = 0; $width < $x; $width++) { for ($height = 0; $height < $y; $height++) { # Define the exact coordinates to place the pixel $width_pos = $width + 1; $height_pos = $height + 1; $width_pos2 = $width + 2; $height_pos2 = $height + 2; $canvas->createRectangle($width_pos * $multipli ,$height_pos * $multip +li , $width_pos2 *$multipli , $height_pos2 * $multipli , -fill => 'bl +ack'); } } } # If it is not the first time in this subroutine, start the thread if ($first_start == "1"){ my $thr1 = threads->create(\&progress_calc, $tot_pix, $one_hun +derd, $passed_pix); } #add one to the variable $passed_pix += 1; # If it is not the first time, Join the thread. if ($first_start == "1"){ $thr1->join(); } } } #call the subroutine hide_progress(); #set the variables when exiting the subroutine $first_start = "1"; $passed_pix = 0; } #subroutine for the progress bar sub progress_calc { # Calculations for progress bar $var1 = $percent_done; $var2 = $passed_pix; $var3 = $one_hunderd; while ($var1 <= 100) { #do some math to define the percent done and update the progress bar. $var1= $var2 / $var3; $win_prog->update(); } } #sub to show the progress bar sub show_progress { $win_prog->deiconify(); $win_prog->raise(); $win_prog->update(); } #sub to hide the progress bar sub hide_progress { $win_prog->withdraw(); }

So basicly what this code does is first do some calculatios to get the percent of the total pixels (I need this to draw a realistic progress bar). Then I'm drawing the image pixel by pixel. While drawing the image, I want to start a thread that is updating the progress bar and doing the calculation for knowing the percent that is drawed already.

So that it the very basic of what I'm trying to do.

Butt, I'm not having any luck in creating a thread that does update the progress bar and so on.

I don't know what I'm doing wrong and where I'm doing it wrong. So could someone please help me in implementing the thread?1

Or maybe somebody has a better idea then using a thread?

Many Many thanks in advance


In reply to Threading Problems by jerre_111

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.