The problem is caused because you are (implicitly) trying to share objects across threads which isn't possible.

However, that doesn't stop you from doing what you are trying to do. It just means that you must adapt your programing style to account for it.

As I understand you, you want to be able to write to a 'file', represented by an IO:String object on one thread and then access the 'output', the string, in another thread. That can be done this way.

#! perl -slw use strict; use threads; use threads::shared; my $var : shared; sub Update{ require 'IO/String.pm'; my $io = IO::String->new( $var ); $io->print( 'Hello world' ); $io->print( $_ ) for 1 .. 10; }; my $th=threads->new( \&Update ); $th->join(); print "'$var'"; __END__ P:\test>368516 'Hello world 1 2 3 4 5 6 7 8 9 10 '

There are a couple of things to note here:

  1. The IO::String object is not shared between threads.
  2. The scalar $var, is shared.
  3. The IOString object $io is created inside the thread that will use it and is only called from within that thread.
  4. I have required the module into the thread that will use it rather than useing it. This is an optimisation. It saves all the code from IO::String being duplicated into every thread created by the program.

    This isn't strictly necessary. You could use IO::String; in the normal way provided any IO::String objects you create are only called from the thread in which the were created.

That said, if you have a choice, there are better ways of sharing data between threads. If you need a filehandle because you are passing this to some other module for it to write to, then you might be better off using a socket rather than an in-memory file this way.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re: Problem with using threads with modules. by BrowserUk
in thread Problem with using threads with modules. by tele2mag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.