--0OAP2g/MAC+5xKAE
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, May 27, 2002 at 11:03:54PM +0900, YANAGAWA Kazuhisa wrote:
> In message <20020527124344.GG17772 / cs.auc.dk>
> larsch / cs.auc.dk writes:
> 
> > > > Is this a bug in Ruby or my brain? :-)
> > > > 
> > > > $ sizedqueue.rb 
> > > > quire 'thread.rb'
> > > > q = SizedQueue.new(0)
> > > > Thread.start { q.pop }
> > > > q.push 1 
> > > 
> > > Maybe it has something to do that you define an empty queue? 
> > > 
> > > This works:
> > > 
> > > q = SizedQuere.new(n)  # n > 0 
> > 
> > But I want an empty queue. I expected pushing the first element on a
> > queue with max 0 elements to block the thread, just as a pushing the
> > 5th element on a full queue with max 4 elements.
> 
> Yes, so the main thread pushing 1 to the queue is blocked.  The other
> thread popping from the empty queue is also blocked.  So no threads
> are running and therefore ruby reports deadlock is occurred since
> blocking threads never become running.  ....What is your problem?
> 
> Your empty queue is empty and will never have an new element.  That's
> your desire?

Clearly, SizedQueue does not currently provide what I am looking for: A worker
thread multiplexer which blocks pushers unless there is a popper ready to take
the element immediatly. 

I was just expecting SizedQueue.new(0) to deliver this functionality by
checking for an ready popper before pushing the element onto the queue. I
see now that it doesnt. This code does what I am looking for:

class EmptyQueue
  def initialize
    @waiting = Queue.new
    @que = Queue.new
  end
  def push(obj)
    @waiting.pop
    @que.push obj
  end
  def pop
    @waiting.push true
    @que.shift
  end
end

Not particularly nice or efficent, but it works :)

-- 
Lars Christensen

--0OAP2g/MAC+5xKAE
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (SunOS)
Comment: For info see http://www.gnupg.org

iD8DBQE88k0uu9sWhhA5M0gRAtOkAJ4jt4RkY+HzDWVut4ZIx+xkpReQtQCfX95T
hiwjoGeCdD24Z72ABostvx0
FX
-----END PGP SIGNATURE-----

--0OAP2g/MAC+5xKAE--