标题: A Question about ithreads..... [打印本页] 作者: apile 时间: 2003-05-09 09:25 标题: A Question about ithreads..... #------------------------------------------------------
最近上面長官要我用multithread的方式將我寫的一個很耗時的程序
加快執行效率,但是我用perl 5.8 的ithread後,發現他在thread間
無法傳送handle
Error如下:
thread failed to start: DBD::Informix::db prepare failed: handle 2
is owned by thread 2003af28 not current thread 20557548
(handles can't be shared between threads and your driver
may need a CLONE method added) at thread.pl line 58.
我原本的想法是利用Thread:ueue由main thread將資料放入輸入
的queue中,然後由前三個thread將queue中的資料取出,並執行
一些測試動作後,放入輸出的queue中。最後由第四個threads將輸出的
queue中的數據,回寫入資料庫中...但是如上面錯誤所見..他建議我要用
CLONE method..不知道有沒有人知道怎麼樣clone?
我很少寫clone方面的東西...有點搞不清楚怎樣 clone..
#!/usr/bin/perl
use threads qw(yield);
use threads::shared;
use Thread:ueue;
my $stream_1 = new Thread:ueue;
my $stream_2 = new Thread:ueue;
my $sql="select * from test";
my $sth_s = $dbh->;prepare(qq{ $sql })|| die "can't preapre $!";
$sth_s->;execute || die "can't execute $!";
my $count = 0;
while(my @aa=$sth_s->;fetchrow_array()){
print "XXXX$count : @aa\n";
$cmd = join("##",@aa);
$stream_1->;enqueue($cmd);
$count++;
# last if (++$count==10);
}