bsdz has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Win32::OLE and threads
by BrowserUk (Patriarch) on Jan 19, 2007 at 18:23 UTC

    Yes. I see exactly the behaviour you describe. Everything seems to run fine, but it never exits. This isn't a surprise. For example, if the OLE code uses the process ID to key stuff off internally, calling it multiple times from the same process on different threads is going to confuse it. And a brief look with a debugger at what is going on, seems to indicate that OLE is getting very confused.

    There are no less that 9 threads running in the process, after your 3 have terminated. Of those, 4 are sitting in RegisterWaitForInputIdle wait states, and will never be awoken.

    I won't pretend to have looked into this very far, but it seems safe to say that using Win32::OLE in conjunction with threads is a no no. At least, if you call OLE from multiple threads.

    It's possible, but I haven't tried it, that like several other packages--Tk, DBI etc.--you might get away with using it in conjunction with threads, provided that you only call it from one thread. It may be that it would only work if you only call it from the initial thread.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32::OLE and threads
by Anonymous Monk on Jan 19, 2007 at 18:35 UTC
    I can't help with your code but I did write one program using threads and Win32::OLE that I finally got to work. This worked for me:

    Don't 'use Win32::OLE'.
    Create all the threads I wanted first.
    Then only in the main thread or at least only in one thread, 'require Win32::OLE' to do what I needed.

    Hope it helps.

    AteWell

      This saved my bacon. I have a multi-threaded script that extracts WMI performance data using Win32::OLE and I was getting crazy results. Failures that didn't make sense until I found this and realised it wasn't thread safe. In my script I "require" and then uninitialize() Win32::OLE around one subroutine within the worker thread and all is good.

      Thanks Monks. I'm replying so that Google may find this quicker and help someone else from the frustration I was having.