bsdz has asked for the wisdom of the Perl Monks concerning the following question:
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?
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.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
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.... 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(); }); ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32::OLE and threads
by BrowserUk (Patriarch) on Jan 19, 2007 at 18:23 UTC | |
|
Re: Win32::OLE and threads
by Anonymous Monk on Jan 19, 2007 at 18:35 UTC | |
by Anonymous Monk on Nov 08, 2010 at 08:40 UTC |