From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 603CA17C05F2 for ; Tue, 30 Oct 2012 11:25:10 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 5F59FEA6B8E for ; Tue, 30 Oct 2012 11:36:59 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 6ADE597A82E for ; Tue, 30 Oct 2012 11:36:59 +0900 (JST) X-Virus-Scanned: amavisd-new at nagaokaut.ac.jp Received: from funfun.nagaokaut.ac.jp ([127.0.0.1]) by funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jE0PQmPUI8nH for ; Tue, 30 Oct 2012 11:36:59 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 30C2D97A825 for ; Tue, 30 Oct 2012 11:36:59 +0900 (JST) Received: from carbon.ruby-lang.org (carbon.ruby-lang.org [221.186.184.68]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id CE4B8952456 for ; Tue, 30 Oct 2012 11:36:57 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [127.0.0.1]) by carbon.ruby-lang.org (Postfix) with ESMTP id C13BF3C21EBAE; Tue, 30 Oct 2012 11:36:55 +0900 (JST) Received: from mail-da0-f50.google.com (mail-da0-f50.google.com [209.85.210.50]) by carbon.ruby-lang.org (Postfix) with ESMTP id 7A8833C21D38A for ; Tue, 30 Oct 2012 11:36:47 +0900 (JST) Received: by mail-da0-f50.google.com with SMTP id z20so2785453dae.37 for ; Mon, 29 Oct 2012 19:36:46 -0700 (PDT) Received: by 10.66.90.36 with SMTP id bt4mr88403247pab.54.1351564606173; Mon, 29 Oct 2012 19:36:46 -0700 (PDT) Received: from higgins.local (c-66-235-47-187.sea.wa.customer.broadstripe.net. [66.235.47.187]) by mx.google.com with ESMTPS id gu5sm3558125pbc.10.2012.10.29.19.36.43 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 29 Oct 2012 19:36:44 -0700 (PDT) Delivered-To: ruby-core@ruby-lang.org Date: Tue, 30 Oct 2012 11:36:48 +0900 Posted: Mon, 29 Oct 2012 19:36:36 -0700 From: Aaron Patterson Reply-To: ruby-core@ruby-lang.org Subject: [ruby-core:48584] Re: [ruby-trunk - Feature #4589][Feedback] add Queue#each() method and include Enumerable Sender: Aaron Patterson To: ruby-core@ruby-lang.org Message-Id: <20121030023636.GA97964@higgins.local> In-Reply-To: References: X-ML-Name: ruby-core X-Mail-Count: 48584 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-core-ctl@ruby-lang.org; help= User-Agent: Mutt/1.5.21 (2010-09-15) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent :x-gm-message-state; bh=75E5h0k5dcjc/pEgf6PlIcFvT/+rLVHOmk8n4EMP3+0=; b=faguMFAw6dUKXGE+aV4bPtHuWNCdTiA6pD67S0d4jwSnZAJNaN9EnUjzofPQjvRxZ+ Gjyq/0dd4H90mXPSvpxM6nP4Y/U/Nzg04of/OlyZjIY5c5VGomozmJGFmuqV3XeWhePk YpS+yajaLv92KSD3Z/9FX8+R1vXDagbaMRKO0YHGR6jlV3cp3OIsBO2nhZV1J8wLCLU2 e56pSFCVb3liqoHf61crGr/rEWX5nS5NH5hhESpCScIMFlhoJON6wOHEMoFdVm9BqMXe QZn5nfO+cyC7isj2gzVZf18wjl6vafbgz57OyFY2WlnOWmGgSFJ3hzJ5ALW77VhlF8SR 2fZw== Content-Disposition: inline X-Gm-Message-State: ALoCoQnhdBHo9w+i5ixlLcLCkQdzJcT9Gr6FO/zcvfnjZOwEa65n1Jl4evbJbq0KqMyjCKKAmTNK Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Precedence: bulk List-Id: ruby-core.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: On Tue, Oct 30, 2012 at 08:44:21AM +0900, ko1 (Koichi Sasada) wrote: > > Issue #4589 has been updated by ko1 (Koichi Sasada). > > Status changed from Assigned to Feedback > Target version set to next minor > > Professional's comments are very welcome. > I can't determine it should be worth or not. I don't think it's a good idea. There are different ways to make a thread-safe `each`, and they have different behavior. For example: class Queue def each(&block) @mutex.synchronize { @que.each(&block) } end end compared to: class Queue def each(&block) @que.dup.each(&block) end end Both provide each, but the semantics are totally different. The first one ensures that no other threads will update the queue, but that blocks any threads from updating the queue. The second one doesn't block the queue, but the values yielded to the block are inconsistent (e.g., a thread could write to the queue after the internal array has been duped). I think it's better to do this: class Queue def synchronize @mutex.synchronize { yield } end def to_a; @que.dup; end end As a user, it's obvious you're getting a copy: queue = Queue.new queue.to_a.each { |item| ... } If you want to block other threads from manipulating the queue while iterating: queue = Queue.new queue.synchronize do queue.to_a.each { |item| ... } end TL;DR: adding `sychronize` and `to_a` would be more powerful and less invasive than adding `each` (IMO). -- Aaron Patterson http://tenderlovemaking.com/