Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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 meditating upon the Monastery: (3)
As of 2024-04-16 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found