in reply to adding a hash to a shared object

I guess that you'll be far better off by sharing the data while it is still in string format instead of sharing it once it is decoded. Also, I think you need to explicitly share every level of a data structure.

Honestly, I'm not sure what you try to accomplish by sharing the data structure. Maybe there is an easier way to accomplish what you want to do without sharing the structure, for example by serializing the structure through a Thread::Queue to many worker threads.

Replies are listed 'Best First'.
Re^2: adding a hash to a shared object
by daverave (Scribe) on Aug 11, 2010 at 11:27 UTC
    Maybe I got it all wrong. I started working with objects but perhaps it's not necessary and I'm just over-complicating things.

    So let me describe the scenario (I probably should have asked for your advice about it earlier)

    My perl script is used to run some kind of a pipeline. I start by reading a JSON file with a bunch of parameters in it. I then do some work - mainly building some data structures needed later and calling external programs that generate some output files I keep references to.

    I usually use a subroutine for each of these steps. Each such subroutine will usually write some data to a unique place that no other subroutine writes to (i.e. a specific key in a hash) and reads data that other subroutines may have generated (when I sued an object I had a large $self with many keys).

    These steps can take a good couple of minutes if done sequentially, but most of them can be run in parallel with some simple logic of dependencies that I have described in an earlier post and I now know how to handle.

    What would you suggest? perhaps a simple script (no objects) with "global" shared variables?

    Thank you

      Pass all the data between your subroutines via Thread::Queue. Each subroutine can be its own thread that reads from one queue and writes to the next queue. That way you have no shared data at all.

        Could you please elaborate a bit?

        I just read Thread::Queue but I'm not sure what you mean. Should I keep a single queue with a single element (e.g. a reference to a hash with all my data)? Then each method dequeues when it starts and enqueue the updated data structure when it finishes?