Hi,

Thank you for the hint with the example of zentara. And thank you for the advice to use Gtk.

I like Tk very much and yesterday arrived the book "Mastering Perl/Tk". So I'd like to stay with Tk at the moment. I nearly finished an application with Tk now and I only need this small thread thing to complete.

Now I could create a solution. Here is the code what is doing what I want.

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my $work:shared = 0; my $die:shared = 0; #create thread before any tk code is called my $thr = threads->create( \&worker ); use Tk; my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW' => sub { &clean_exit }); my $button_stop = $mw->Button(-text => 'Stop thread', -command => sub { $work = 0; })->pack(); my $button_start = $mw->Button(-text => '(Re)Start thread', -command => sub { $work = 0; sleep(1); $work = 1; })->pack(); MainLoop; sub clean_exit { my @running_threads = threads->list; if (scalar(@running_threads) > 1) { print "ERROR: Too many threads are active. This was not planne +d!\n"; } elsif (scalar(@running_threads) == 1) { $work = 0; $die = 1; $thr->join; exit; } else { print "ERROR: There should be at least one thread started!\n"; } } # no Tk code in thread sub worker { my $i; THREAD_START: while($work == 0) { # just wait if( $die == 1 ) { return; } } $i = 0; while($work == 1) { print $i . "\n"; $i++; } if( $die == 0 ) { goto THREAD_START; } }

Hopefully you monks can tell me how to improve this code. Using goto is bad and waiting for an event in a loop is also not the best style and costs a lot of performance. But I don't know how to solve it in a different way.

Greetings

Dirk


In reply to Re^2: Thread problems by Dirk80
in thread Thread problems by Dirk80

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.