Esteemed monks

I know Win32::OLE is not thread safe so this question is maybe pushing the envelope a little. I am using ActiveState Perl 5.8.8 on Win32. When I run the following script on my XP PC it does not exit. In fact it runs everything up to the point before exit. Does anyone experience the same problem?

use strict; use warnings; use threads; use threads::shared; use Thread::Semaphore; use Win32::OLE qw(in); my $debugSemaphore = Thread::Semaphore->new; exit !runThreadGroup();; sub runThreadGroup { my %threads : shared; # workaround Win32::OLE + threads bug my %exitcodes : shared; foreach my $i (0..2) { my $tid = threads->create(sub { debug("Starting thread"); $exitcodes{threads->tid()} = map { debug("-".join(',' , $_->ProcessId, $_->Name)); } in(Win32::OLE->GetObject("winmgmts:") ->ExecQuery("SELECT * FROM Win32_Process")); delete $threads{threads->tid()}; debug("Ending thread"); }); $threads{$tid->tid()} = undef; } sleep 1 while keys(%threads) > 0; print "Thread exit codes: ".join(',', values(%exitcodes))."\n"; foreach my $ec (values(%exitcodes)) { return 0 if $ec != 0; } return 1; } sub debug { my ($message) = @_; $debugSemaphore->down; print STDERR "# [".threads->tid."] $message\n"; $debugSemaphore->up; return 1; } __DATA__ Perl 5.8.8 MSWin32-x86-multi-thread ActivePerl Build 817
Update: Thanks for your help. For future monks reading this thread one way around this problem is to only load Win32::OLE in each thread. You will also need to uninitialize the module. I.e.
... my $tid = threads->create(sub { require Win32::OLE; import Win32::OLE qw(in); debug("Starting thread"); $exitcodes{threads->tid()} = map { debug("-".join(',' , $_->ProcessId, $_->Name)); } in(Win32::OLE->GetObject("winmgmts:") ->ExecQuery("SELECT * FROM Win32_Process")); delete $threads{threads->tid()}; debug("Ending thread"); Win32::OLE->Uninitialize(); }); ...
and remove the "use Win32::OLE qw(in)". The downside of this method is that you cannot load Win32::OLE in the main thread or at least you cannot do this till after your child threads have started.

In reply to Win32::OLE and threads by bsdz

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.