From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28044 invoked by alias); 24 Sep 2009 14:57:55 -0000 Received: (qmail 28029 invoked by uid 22791); 24 Sep 2009 14:57:55 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 24 Sep 2009 14:57:50 +0000 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n8OEvlRG009531; Thu, 24 Sep 2009 10:57:48 -0400 Received: from [IPv6:::1] (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n8OEvkda018770; Thu, 24 Sep 2009 10:57:46 -0400 Message-ID: <4ABB88EA.1030703@redhat.com> Date: Thu, 24 Sep 2009 14:57:00 -0000 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.4pre) Gecko/20090922 Shredder/3.0pre MIME-Version: 1.0 To: Chris Lattner CC: Richard Henderson , "Vincent R." , "gcc@gcc.gnu.org >> GCC" Subject: Re: apple blocks extension References: <636199343f6e2ed0f0a482615b4d1435@mail.smartmobili.com> <4AAFBAF2.3050209@redhat.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-09/txt/msg00512.txt.bz2 On 09/15/2009 12:35 PM, Chris Lattner wrote: > The second major feature of Blocks vs c++ lambdas is that they can be > "copied onto the heap". This allows things like "Grand Central Dispatch" > to work: you can write code that executes blocks asynchronously or on > other threads/work queues (after the function containing the block has > returned). A simple example is: > > void print_on_different_thread(int X) { > run_asynch(^{ > printf("Hi %d\n", X); > }); > } > > With lambdas, the closure would be go out of scope when > print_on_different_thread returns, but blocks allows "run_asynch" to > extend the lifetime of the block. The lambda equivalent would be void print_on_different_thread(int X) { run_asynch([=]{ printf("Hi %d\n", X); }); } since X is captured by copy, run_asynch can do whatever it wants with the closure and not worry about the original X going away. The only difference between blocks and lambdas here seems to be where you decide to copy the locals off the stack. Jason