Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

multithreading - Perl: share complex data-structure between threads

I like to share a complex-datastructure between threads. As far I know that is not possible with threads:shared (only basic types are shareable).

So I think about serialize/deserialize the structure with JSON or Storable so it is just a string that I can share perfectly. But I need to unpack it before use and pack it after a change.

  • Is that a common way to work on that problem?

  • Are there better ways?

  • Whould you prefer JSON or Storable or something else?

Thanks for help!

EDIT

I just did some tests with Storable and JSON. JSON is quicker and produces smaller serialized strings. I did not expect that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When dealing with this problem, I use Thread::Queue to pass my objects around, and usually use Storable to serialise.

I haven't bothered doing performance comparisons, because usually my data-passing overhead isn't the limiting factor.

Note - the key advantage of Storable is that it allows some limited object support (Not - be careful - it only works if your object is self contained):

#!/usr/bin/env perl
use strict;
use warnings;

package MyObject;

sub new { 
   my ( $class, $id ) = @_; 
   my $self = {};
   $self -> {id} = $id; 
   $self -> {access_count} = 0; 
   bless $self, $class;
   return $self;
}

sub access_thing { 
   my ( $self ) = @_;
   return $self -> {access_count}++; 
}

sub get_id { 
    my ( $self ) = @_;
   return $self -> {id}; 
}

package main; 

use threads;
use Thread::Queue;

use Storable qw ( freeze thaw );

my $thread_count = 10;

my $work_q = Thread::Queue -> new; 

sub worker  {
   while ( my $item = $work_q -> dequeue ) {
      my $obj = thaw ( $item ); 
      print $obj -> get_id, ": ", $obj -> access_thing,"
";    

   }
}

for (1..$thread_count) {
   threads -> create (&worker); 
}

for my $id ( 0..1000 ) {
   my $obj = MyObject -> new ( $id ); 
   $work_q -> enqueue ( freeze ( $obj ) );
}

$work_q -> end;

$_ -> join for threads -> list; 

If JSON would limit you to array/hash data structures - which may be fine for your use case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...