I find that if I run a script that uses thread and that invokes my XS
module, it works if each thread performs a require on the module, but
if I perform a C<use> first in the script it does not work. (The posted
sample code below runs, but not if I uncomment the use statement and 
comment the require and import statements). 

The error is "CoInitialize has not been called" which comes from COM
in Windows. CoInitialize is called from a routine "initialize" that
is called from my BOOT section, which obviously is only called once
for the whole script, but needs to be called once for each thread.

Provided, that is, that a C<use> is the right way to use a module in a
threaded script. So that is my first question: is the script below
good enough, or should I do better?

Assuming that I should do better, what is the best way to do better? 
I guess that I'm from my new method I could check whether I have run
my BOOT routine, and run it if not, but it sounds a little funny. (I
think will blissfully ignore the case than a object is shared between
threads. That's seems like a poor idea for a database connection.)

   use threads;
   #use MSSQL::OlleDB;
   use strict;
   
   use vars qw($save);
   
   foreach my $i (1..10) {
      my $xx = threads->create(\&blaha, $i);
   }
   foreach my $i (1..10) {
      my $t = threads->object($i);
      if ($t) {
         my $ret = $t->join;
         print "Thread $i returned '$ret'\n";
      }
      else {
         print "Thread $i does not exist.\n";
      }
   }
  
   sub blaha {
      my($in) = @_;
      $save = $in;
      require MSSQL::OlleDB;
      import  MSSQL::OlleDB;
      my $self = threads->self;
      my $tid = $self->tid;
      my $sql = sql_init();
      $sql->sql("WAITFOR DELAY '00:00:01'");
      print "Blaha - StupidThreadTest($save) - I have threadid = $tid.\n";
      return '$save';
   }



-- 
Erland Sommarskog, Stockholm, [EMAIL PROTECTED]

Reply via email to