List Info

Thread: Free to wrong pool




Free to wrong pool
user name
2006-07-19 13:50:49
You can get the version number using:
<code>
    perl -Mthreads -e 'print
"$threads::VERSION\n"'

</code>
As of this posting, the lastest version is 1.36.
<br /><br />

To keep non-thread-safe modules out of threads, you can
create the threads in a BEGIN block before including the
non-thread-safe modules, and then feed data to the threads
using queues:

<code>
use threads;
use Thread::Queue;

# Thread function that gets data via a queue
# (NOTE:  Must be coded up before the BEGIN block.)
sub thr_func {
    my $que = shift;

    # Wait for data
    my $data = $que->dequeue();

    # Do work on $data
    ...
}


# Create threads and queues
# (NOTE:  Threads must be created in a BEGIN block before
#         'use'ing any non-thread-safe modules.)
my %threads;
BEGIN {
    for (1..5) {
        my $que = Thread::Queue->new();
        my $thr = threads->create('thr_func', $que);
        $threads{$thr->tid()} = [ $thr, $que ];
    }
}


# Get data using non-thread-safe modules
use Non::Thread::Safe::Module;

foreach my $tid (keys(%threads)) {
    # Get data
    my $data = ...

    # Give data to thread
    my ($thr, $que) = $threads{$tid};
    $que->enqueue($data);
}


# Wait for all threads to finish
$_->join() foreach threads->list();

</code>

Thread::Queue only works with scalars.  If you need to pass
anything more complex to the threads, then use
Thread::Queue::Any.

<br /><br />

In regards to one of your earlier posts, note the last
statement for waiting on the threads.  You don't have to
worry about joining the main thread:
'threads-&gt;list()' only
returns non-detached threads, and the main thread is
detached.

To write a respons, access
http://ww
w.cpanforum.com/response_form/2647

To see the full thread, access
http://www.cpan
forum.com/threads/2642

--
You are getting this messages from www.cpanforum.com
To change your subscription information visit http://www.cpanforum.
com/mypan/

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )