in reply to Re^3: Cloning shared hashref
in thread Cloning shared hashref

I ran in exactly the same problem, I wanted to serialize a shared variable with YAML::XS. I reversed the shared_clone function and named it unshared_clone. The advantage is that references stay references (less memory consumption) and circular references don't blow up your script. I put it on github https://github.com/jwba/threads-shared-util.

Replies are listed 'Best First'.
Re^5: Cloning shared hashref
by BrowserUk (Patriarch) on Mar 16, 2011 at 23:04 UTC

    Could I ask you to explain the program requirements that led to the need for this routine and its particular feature set?

      Sure. I was running some functions in separate threads, returning graph-like structures (feature annotations of genome data), e.g.

      my $gene = { lots => 'of', data => 'entries' }; my $transcript = { 'even' => 'more', data => 'entries', gene => $gene +}; $gene->{transcript} = [ $transcript ];

      I put everything in a result queue of type Thread::Queue. I dequeue and freeze the results to a YAML file after the program finishes, so that I don't have to calculate everything again and again. Unfortunately, Thread::Queue does a shared_clone on everything one throws at it.

      This fact leads to the mentioned 'program requirements':

      1. Handle circular references
      2. Save some space with reusing references
      I hope this answers your question.