[Mono-list] yield

Rodrigo B. de Oliveira rodrigobamboo at gmail.com
Thu Sep 15 15:40:34 EDT 2005


On 9/14/05, Bernhard Herzog <schwimmlehrer at gmail.com> wrote:
> > It's a bug in gmcs.
> >
> > However, using 'lock' and 'yield' together doesn't sound like a good
> > idea to me.  The standard doesn't specify anything about releasing and
> > re-acquiring the lock around a yield[1]: so, the lock is very
> > long-lived.
> 
> 
> I have filed a bug report.
> 
> >From the Microsoft C# Programmers Reference:
> Unsafe blocks are not allowed
> 
> My naive unterstanding of how yield works was that code until the loop is
> executed (i.e. the lock is aquired), then the code inside the foreach (the
> one that called the function containing yield) is called and finally the
> code after my loop is called (i.e. the lock is released).

Yes, that's an understandable assumption but it is not how it works.
The actual flow is a little more complicated. Every time the code hits
an 'yield' it leaves the method and sets the yield return argument as
the current item for the iteration. When the code needs to consume
another item the method is reexecuted starting from the line just
after the last executed yield statement (the compiler emits code to
branch to that point upon reentering the method). At least that's how
it works in boo and I'm (naively?) assuming the c# model would be
pretty much the same.

-- 
bamboo
http://blogs.codehaus.org/people/bamboo/
Got objects? http://www.db4o.com/


More information about the Mono-list mailing list