I am working on an application that uses threads and I tried using
Win32::Console::ANSI for some colored text output.  This seemed to work
fine, except that it will hang if you try to join a thread that has
completed:

#!/usr/bin/perl -w
use strict;
use threads;
use threads::shared;
use Win32::Console::ANSI;
use Term::ANSIColor;

$|++;

my @c= qw/ red green yellow /;

foreach (0..2) {
  my $thread = threads->new(\&thread_sub, $_);
  $thread->join();
}

sub thread_sub {
  my $cnt = shift;
  my $id = threads->self->tid;
  for (1..3) {
    sleep 1;
    print "this is a test: $id\n";
  }
}

The above will fail after the first thread.  This came from a discussion I
was having with the author of Win32::Console::ANSI, Jean-Louis Morel, who
has been very helpful.

He tracked the problem to Win32::Console:

------------------
Bad news: it is the module Win32::Console itself that is not thread-safe :-(
The threads runs but STDOUT is unexpectedly closed (?) after the first
thread ended.
Try:

#!/usr/bin/perl -w
use strict;
use threads;
use Win32::Console;

$|++;

my $OUT = new Win32::Console(STD_OUTPUT_HANDLE);

foreach (0..2) {
   my $thread = threads->new(\&thread_sub, $_);
   $thread->join();
}

sleep 10;
print "End.\n";  # print nothing!

sub thread_sub {
   my $cnt = shift;
   my $id = threads->self->tid;
   for (1..3) {
     sleep 1;
     print "this is a test (print): $id\n";
     $OUT->Write("this is a test (Write): $id\n");
   }
}
__END__

Even if one delete the line $OUT->Write(...etc, the prints fails.
It's a serious bug because the module Win32::Console is just an
interface with the Win32 API: the bug is probably in the Windows console.
Sorry, I cannot do more, I don't have a workaround :-(

Regards.

-- 
J-L.
------------------

I assume that Win32::Console has some XS stuff going on, and from what
I've read it's not unlikely that that could cause thread-safe problems.

I'm curious if anyone else has run into this, or better yet, if anyone has
a solution.  Although it's not necessary for me to use these modules, it
would be nice.  I have emailed the author of Win32::Console, Aldo Calpini,
but figured I would drop a note on this list, too.

Any help is appreciated.

andy


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to