Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yield returns within lock statement

if i have a yield return in a lock statement does the lock get taken out on each yield (5 times in the example below) or only once for all the items in the list?

Thanks

    private List<string> _data = new List<string>(){"1","2","3","4","5"};     private object _locker =new object();     public IEnumerable<string> GetData()     {         lock (_locker)         {             foreach (string s in _data)             {                 yield return s;             }         }     } 
like image 490
DayOne Avatar asked May 17 '10 08:05

DayOne


People also ask

What is yield return in C sharp?

The yield return statement returns one element at a time. The return type of yield keyword is either IEnumerable or IEnumerator . The yield break statement is used to end the iteration. We can consume the iterator method that contains a yield return statement either by using foreach loop or LINQ query.

What is yield return?

The yield is the income the investment returns over time, typically expressed as a percentage, while the return is the amount that was gained or lost on an investment over time, usually expressed as a dollar value.

When would you use the yield break statement?

You can access the EvenNumbers static property to display the even numbers between 1 and 10 at the console window using the code snippet given below. You can use the "yield break" statement within an iterator when there are no more values to be returned. The "yield break" statement is used to terminate the enumeration.

Does yield break return null?

"yield break" breaks the Coroutine (it's similar as "return"). "yield return null" means that Unity will wait the next frame to finish the current scope. "yield return new" is similar to "yield return null" but this is used to call another coroutine.


2 Answers

BEGIN EDIT
Please refer to the code in the community wiki provided by @EZI which is easier to read / cleaner.
END EDIT

Sorry to resurrect this from the dead, but reading the accepted answer by Daniel, and then testing it myself I though that at least those 10 people up-voting should at least know it's completely wrong.

The answer is: The lock is NEVER released between each yeald return.
NOTE: It is however released when the enumerator is done, i.e. when the foreach loop ends.

Daniel's answer is wrong in claiming that the lock is taken more than once. That is because Daniel's code is not multi-threaded, it would always compute the same way. The lock in that code is taken only once, and since it's the same thread, it's always the same lock.

I took @Daniel's code from his answer, and changed it to work with 2 threads, one for List1 and another thread created for each iteration of List2.

As you can see once t2 thread is started, the threads would dead-lock, since t2 is waiting on a lock that would never be released.

The Code:

void Main() {     object locker = new object();     IEnumerable<string> myList0 = new DataGetter().GetData(locker, "List 0");     IEnumerable<string> myList1 = new DataGetter().GetData(locker, "List 1");     IEnumerable<string> myList2 = new DataGetter().GetData(locker, "List 2");      Console.WriteLine("start Getdata");     // Demonstrate that breaking out of a foreach loop releasees the lock     var t0 = new Thread(() => {         foreach( var s0 in myList0 )         {             Console.WriteLine("List 0 {0}", s0);             if( s0 == "2" ) break;         }     });     Console.WriteLine("start t0");     t0.Start();     t0.Join(); // Acts as 'wait for the thread to complete'     Console.WriteLine("end t0");      // t1's foreach loop will start (meaning previous t0's lock was cleared     var t1 = new Thread(() => {         foreach( var s1 in myList1)         {             Console.WriteLine("List 1 {0}", s1);             // Once another thread will wait on the lock while t1's foreach             // loop is still active a dead-lock will occure.             var t2 = new Thread(() => {                 foreach( var s2 in myList2 )                 {                     Console.WriteLine("List 2 {0}", s2);                 }             } );             Console.WriteLine("start t2");                       t2.Start();             t2.Join();             Console.WriteLine("end t2");                     }     });     Console.WriteLine("start t1");     t1.Start();     t1.Join();     Console.WriteLine("end t1");     Console.WriteLine("end GetData"); }  void foreachAction<T>( IEnumerable<T> target, Action<T> action ) {     foreach( var t in target )     {         action(t);     } }  public class DataGetter {     private List<string> _data = new List<string>() { "1", "2", "3", "4", "5" };      public IEnumerable<string> GetData(object lockObj, string listName)     {         Console.WriteLine("{0} Starts", listName);         lock (lockObj)         {             Console.WriteLine("{0} Lock Taken", listName);             foreach (string s in _data)             {                 yield return s;             }         }         Console.WriteLine("{0} Lock Released", listName);     } } 
like image 121
Lockszmith Avatar answered Oct 14 '22 11:10

Lockszmith


Edit: This answer was wrong, but I can't delete it as it was marked as correct. Please see @Lockszmith's answer below for the correct answer.

Paraphrased:

The lock is NEVER released between each yeald return. NOTE: It is however released when the enumerator is done, i.e. when the foreach loop ends.

End Edit

Original Answer (wrong):

In your scenario, the lock will only be taken once. So in short, only once. However, you are not dealing with any shared resources. When you start to deal with shared resources like in the console app below, some interesting things happen.

You will see from the results that the lock is temporarily released on each yield. Also, note that the lock on List 1 is not released until all of the items have been written to the console, demonstrating that the GetData() method is executed partially with each iteration of the loop and that the lock must be released temporarily with each yield statement.

    static void Main(string[] args)     {         object locker = new object();         IEnumerable<string> myList1 = new DataGetter().GetData(locker, "List 1");         IEnumerable<string> myList2 = new DataGetter().GetData(locker, "List 2");         Console.WriteLine("start Getdata");         foreach (var x in myList1)         {             Console.WriteLine("List 1 {0}", x);             foreach(var y in myList2)             {                 Console.WriteLine("List 2 {0}", y);             }         }         Console.WriteLine("end GetData");         Console.ReadLine();     }      public class DataGetter     {         private List<string> _data = new List<string>() { "1", "2", "3", "4", "5" };          public IEnumerable<string> GetData(object lockObj, string listName)         {             Console.WriteLine("{0} Starts", listName);             lock (lockObj)             {                 Console.WriteLine("{0} Lock Taken", listName);                 foreach (string s in _data)                 {                     yield return s;                 }             }             Console.WriteLine("{0} Lock Released", listName);         }     } } 

Results:

            start Getdata             List 1 Starts             List 1 Lock Taken             List 1 1             List 2 Starts             List 2 Lock Taken             List 2 1             List 2 2             List 2 3             List 2 4             List 2 5             List 2 Lock Released             List 1 2             List 2 Starts             List 2 Lock Taken             List 2 1             List 2 2             List 2 3             List 2 4             List 2 5             List 2 Lock Released             List 1 3             List 2 Starts             List 2 Lock Taken             List 2 1             List 2 2             List 2 3             List 2 4             List 2 5             List 2 Lock Released             List 1 4             List 2 Starts             List 2 Lock Taken             List 2 1             List 2 2             List 2 3             List 2 4             List 2 5             List 2 Lock Released             List 1 5             List 2 Starts             List 2 Lock Taken             List 2 1             List 2 2             List 2 3             List 2 4             List 2 5             List 2 Lock Released             List 1 Lock Released             end GetData 

However, he really cool thing here is the results. Note that the line, "start GetData" occurs after the call to DataGetter().GetData() but before everything that occurs within the GetData() method. This is called deferred execution and it demonstrates the beauty and usefulness of the yield return statement: Anywhere within your outer loop you can break out of the loop and there will be no more calls to the inner loop. This means that you don't have to iterate the entire inner loop if you don't have to and it also means that you will start to get results to your outer loop earlier.

like image 29
Daniel Dyson Avatar answered Oct 14 '22 12:10

Daniel Dyson