Threads and OO Question

From: Edward Wildgoose (Ed+nospam_at_ewildgoose.demon.co.uk@)
Date: 10/14/03


Date: Tue, 14 Oct 2003 14:41:34 GMT

I have a peculiar problem with threads on Perl 5.8 (activestate on Win32)

My sample code is below, however, the problem is basically that if I run
this under the debugger then if "works" as expected. Works in this case
defined as some output goes to stdout.

However, if I simply run the program from the command line, then I only get
output from the thread running "MyPackage->start"... There is no output
from the main thread. Any ideas why (does it happen to anyone else? Linux?)
Is this to do with one of the threads grabbing stdout and the other not
being able to access it?

Secondly is this the "right" way to do thread programming? The "problem" I
am trying to solve is to have the main start thread showing a GUI interface,
where the MyPackage class is then a worker class which goes off and does
something. Then the GUI will periodically access the properties of
"MyPackage" to determine state and progress (and update the GUI). FWIW,
there is a deliberate attempt to keep the two processes seperate hence not
relying on the any abilities the GUI toolkit might provide to do background
processing.

Thanks

Ed W

>>>>>>>>>>>>>>>>>>>>>>>>>>>>

use threads;
use threads::shared;

use strict;

package MyPackage;

my $counter : shared;
$counter = 0;

sub new {
  my $class = shift;
  my $this = {COUNTER => 0};
  threads::shared::share($this->{COUNTER});

  $counter = 0;

  return bless($this, $class);
}

sub start {
  my $this = shift;

  while (1) {
    $counter += 1;
    $this->{COUNTER} += 1;
    sleep 1;
    print STDOUT "In Counter: $counter \r\n";
  }
}

sub getCounter {
  my $this = shift;
  return $this->{COUNTER};
# return $counter;
}

package main;

sub MainLoop {
  my $a;
  my $a = MyPackage->new();

  my $thr = threads->new(sub{$a->start();});
  sleep 1;

  while (1) {
    my $count = $a->getCounter();
    print STDOUT $count;
    sleep 1;
  }

  my @ReturnData = $thr->join;
  print "Thread returned @ReturnData";
}

MainLoop();



Relevant Pages

  • Re: FolderBrowserDialog and Apartment Model Misery
    ... GUI interaction should be done on the Main Thread ... Private Sub Button1_Click(ByVal sender As System.Object, ... Using FbDlg As New FolderBrowserDialog ... the worker-bee threads use FolderBrowserDialog. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: scope again
    ... >> sub fchsym{#returns the symbol for map printing. ... I am pretty good with perl and have been ... I have seen GUI pages with C++ but I can't find any good books on doing ...
    (comp.lang.perl.misc)
  • Re: ideas on how to make a test suite
    ... I'm just trying to do a very simple GUI case at this point: ... executes the tests, and make a sub that calls all the Public test subs, to ... Using MACROS & Automation ... In these experiments, the base Access program opens, but I ...
    (microsoft.public.access.modulesdaovba)
  • RE: Status Bar Updating and SQL Connections
    ... the click handler looking something like this: ... > 1 for the SQL query and that didn't make a difference. ... >> The problem is that you can't update the GUI from different threads. ... >> Private Sub DoWork() ...
    (microsoft.public.dotnet.framework.windowsforms)