[Mono-list] yield

Raja R Harinath rharinath at novell.com
Wed Sep 14 06:00:20 EDT 2005


Hi,

"Bernhard Herzog" <schwimmlehrer at gmail.com> writes:

> I wanted to use yield (like Miguel did it in HttpApplication), but I
> need to lock by data. Is this not allowed?
>
> static object lockit = new Object();
>
> public static IEnumerable GetData()
> {
>   lock (lockit)
>   {
>    for (int i = 0; i < 10; i++)
>     yield return true;
>   }
> }
>
> I am getting this error (with Mono, it works with MS.Net):
>
> Unhandled Exception: System.Exception: Trying to emit a local from a
> different ILGenerator.

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.

- Hari

[1] Even if it did, we probably wouldn't've gained much, since, then
    we'd have to re-establish several invariants that we spent some
    effort setting up before the 'yield'.


More information about the Mono-list mailing list