Siegfried Heintze wrote:
I'm looking at the source code for queue.pm

Perl is case sensitive so that should be Queue.pm.


and I'm quite surprised at how
short it is. I have some questions about the syntax.

(1)     What does this colon (": shared") mean on line 69? I have not seen a
colon used like this before.

That is the syntax for attributes which are described in:

perldoc perlsub
perldoc perlthrtut

See also:

perldoc threads
perldoc threads::shared
perldoc perlothrtut


  67 sub new {
  68     my $class = shift;
  69     my @q : shared = @_;
  70     return bless [EMAIL PROTECTED], $class;
  71 }

(2)     From the main program, I'm calling "my $Q = Thread::Queue->new();".
Now there are no arguments except $class. What does @q get? An empty array?

perldoc Thread::Queue [snip] FUNCTIONS AND METHODS new The "new" function creates a new empty queue. ^^^^^^^^^^^^^^^

(3)     What are we pushing on here on line 90? I don't see an array defined
anywhere. When we bless an object, we basically have hash table, correct?
How is it that we are allowed to push on a hash table? Is it legal to bless
an array instead of a hash table?

perldoc perltoot [snip] Alternate Object Representations Nothing requires objects to be implemented as hash references. An object can be any sort of reference so long as its referent has been suitably blessed. That means scalar, array, and code references are also fair game.

       A scalar would work if the object has only one datum to hold.  An array
       would work for most cases, but makes inheritance a bit dodgy because
       you have to invent new indices for the derived classes.

       Arrays as Objects

       If the user of your class honors the contract and sticks to the
       advertised interface, then you can change its underlying interface if
       you feel like it.  Here's another implementation that conforms to the
       same interface specification.  This time we'll use an array reference
       instead of a hash reference to represent the object.


  87 sub enqueue {
  88     my $q = shift;
  89     lock(@$q);
  90     push @$q, @_  and cond_signal @$q;
  91 }

(4)     I wish I knew why this code does not work. It is so simple. There
must be a bug in line 76 because it never returns from a call to dequeue.

perldoc Thread::Queue [snip] FUNCTIONS AND METHODS new The "new" function creates a new empty queue.

       enqueue LIST
               The "enqueue" method adds a list of scalars on to the end of
               the queue.  The queue will grow as needed to accommodate the
               list.

       dequeue The "dequeue" method removes a scalar from the head of the
               queue and returns it. If the queue is currently empty,
               "dequeue" will block the thread until another thread "enqueue"s
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               a scalar.
               ^^^^^^^^

       dequeue_nb
               The "dequeue_nb" method, like the "dequeue" method, removes a
               scalar from the head of the queue and returns it. Unlike
                                                                 ^^^^^^
               "dequeue", though, "dequeue_nb" won't block if the queue is
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               empty, instead returning "undef".
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Do you suppose this means there is a bug in the implementations of cond_wait
for 5.8 of ActiveState perl? When I discovered that my code was not working
correctly, I typed in the code from Larry Wall's "Programming Perl" and that
did not work either. Both my code and Larry's hang on this dequeue function,
even when there is something in the queue.

Are you sure that there is something in the queue? Have you tried it with the dequeue_nb method instead?



John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to