roben has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I'm using perl since some time now and I just wanted to see some new aspects of it. So i started writing a Tk-Based mp3-player(-interface) in oop using threads.
The question is: How can i share objectes between threads?
Example:
I have my main application, a gui-object, a player and one object for each mp3-file (in @mp3s). The player is a loop that is started in a thread and listens on a Thread::Queue for instructions, polls the player (mpg321) and stores some information in another queue. The gui has its own loop.
The only way i figured out to let the player play a song was to push its filename directly in the thread::queue because @mp3s can not be shared. I think this is firstly ugly, secondly not really oop and i don't like it personally.
So if i could call a method in the player-module providing a shared mp3-object all my problems where solved...

Note: writing "my @mp3s : shared;" and filling it later with objects makes Perl say: "Invalid value for shared scalar"

Thanks in advance, Robert
  • Comment on trouble writing threaded and object oriented

Replies are listed 'Best First'.
Re: trouble writing threaded and object oriented
by jbrugger (Parson) on Jul 20, 2005 at 10:40 UTC
    I don't think you can;
    as threads::shared states:
    bless is not supported on shared references. In the current version, bless will only bless the thread local reference and the blessing will not propagate to the other threads. This is expected to be implemented in a future version of Perl.
    update
    Hmm... there seems to be Pots::SharedObject, but as i look at the version number it's in early stages.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
      Thanks! I searched CPAN and im sure it was not there 2 hours ago ;-) Ill check it out, results will be posted here!

      Result:

      Pots::SharedObject did'nt help much here, because it shares all instances of a class and the thing i really don't like is that you have to modify the class and it's constructor to make it work.
      I guess i'll better wait until Perl is ready for "my $obj : shared = new My::Class;" because that's the way it has to be.
Re: trouble writing threaded and object oriented
by anonymized user 468275 (Curate) on Jul 20, 2005 at 15:27 UTC
    An alternative method of interprocess communication is via shared memory, as described in Re: Reliable asynchronous processing, its links and its references to the big camel book.

    One world, one people