Dear Monks,

I am trying to get to grips with Perl-threads and I have the following problem on a 5.8-Perl that I have to use (where you don't seem to be able to share objects):

What I want to do is use Thread::Queue to pass data from one thread to another (one enqueues, the other dequeues).

Now this is very easy to do if you can create your Queue before you spawn the threads as then both threads have acccess to the queue.

What I want to do now is to create a thread (let's call it thread1) and then some time later create another thread (thread2) together with a queue and use the queue to pass data between thread1 and thread2.

What I cannot figure out is how thread1 can get access to the queue (which is an object and so cannot be shared in an easy way)...

Is that simply not possible? How would I use other ways of inter-thread-communication to pass data from thread1 to thread2?

Evidently in the case of just two threads you can arrange for the queue to be created beforehand but ultimately I want to deal with a situation where you do not know in advance how many threads there will be in the end (e.g. a new thread gets created for every network-connection) and all the new threads have to be able to dequeue data that a "producer"-thread fills.

Many thanks for all suggestions...


In reply to threads and queues by morgon

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.