From: Aaron Patterson Date: 2012-04-14T11:08:46+09:00 Subject: [ruby-core:44355] Re: [ruby-trunk - Feature #6293][Open] new queue / blocking queues --W/nzBZO5zC0uMSeA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Apr 14, 2012 at 10:23:56AM +0900, Eric Wong wrote: > "tenderlovemaking (Aaron Patterson)" wrote: > > Whenever I use queues, I either use them in a blocking or non-blocking > > manner only, so I have separated them in to two classes Thread::Queue, > > and Thread::BlockingQueue. >=20 > I don't think queues should be limited to strictly > blocking/non-blocking. >=20 > Outside of Ruby, but I've occassionally needed queues that could > toggle between blocking/non-blocking (even in the same thread) > to enforce some sort of fairness. Something like: >=20 > while task =3D queue.take > while task.run_one_timeslice >=20 > if pending_task =3D queue.trytake # non-blocking >=20 > # reschedule task if another task shows up > queue.push(task) > task =3D pending_task > end >=20 > end > end The patch I submitted includes a method `poll` which will not block if a timeout isn't supplied. In your case, I would rewrite as: queue =3D Thread::BlockingQueue.new while task =3D queue.shift while task.run_one_timeslice =20 if pending_task =3D queue.poll # non-blocking =20 # reschedule task if another task shows up queue.push(task) task =3D pending_task end =20 end end > Also, I very often use queues (often Unix pipes) where one end is > blocking and the other end is not (though you don't implement > non-blocking push at all) Both of these queues should have non-blocking push since neither is bounded. If this patch is applied, I'll add sized queues which can block. :-) --=20 Aaron Patterson http://tenderlovemaking.com/ --W/nzBZO5zC0uMSeA Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (Darwin) iQEcBAEBAgAGBQJPiNw2AAoJEJUxcLy0/6/G06cH+wSevJVWPeSboUFPF2Cach6S xAA2RTM9GoJWj1LsxXy4L8lR6a+H0obFDTJ4JG7JBJ56GQg/0xSqSRFmJJBLj/Fq EZRd4Vy6oRA+z/t4x32pmvbq0s4BvL7l1+T9MOiAUMRcOZB/Lu4Ty0yawjqMaDQQ Vu5YGBPh7V8jlLtyL3uZzJ1/ORKyNj9xXXei+ivy7swzeQFtYr3ZOD2l7tuKBtFK 77m6h4JdVumJcIkaPPHdYAdSzZQd5+BFl0vnBxsoKfvMPhHzIceFKNa056e1QAEZ 2OSBumtl6nba+JIcs5i4DY4y9qV/KrnJLry6oJSpl/+EPhVYu+cswsn7/geBz1I= =2PWe -----END PGP SIGNATURE----- --W/nzBZO5zC0uMSeA--