c# - yield returns within lock statement -


If I have a yield return in a lock statement then the lock takes every product (in the example below 5 Fold)) or only once for all the items in the list?

Thanks

  Private list & lt; String & gt; _data = new list & lt; String & gt; () {"1", "2", "3", "4", "5"}; Private object _locker = new object (); Public IEnumerable & lt; String & gt; GetData () {lock (_locker) {foreach (strings in _data) {yield returns; }}}  

edit: The answer was wrong, but I can not remove it because it was marked as correct. Please see the answer given to @Looksmith below for the correct answer.

Paraphrased:

The lock is never Note issued between each preceding withdrawal: This is issued when the calculator is done, i.e. when foreach The loop ends. Original answer (wrong):

In your scenario, the lock will be taken only once. So in a nutshell, only once, however, you are not working with any shared resources. When you start dealing with shared resources like the console app below, there are some interesting things.

As you will see from the results that the lock has been released temporarily on each yield. Also, note that the lock on list 1 is not released until all the items are written in the console, indicating that the GetData () method is partially executed, the recurrence of each loop Together and lock should be temporarily released with each Yield statement.

Fixed zero main (string [] args) {object locker = new object (); IEnumerable & LT; String & gt; MyList1 = New Datasigator () GetData (Locker, "List 1"); IEnumerable & LT; String & gt; MyList2 = new datagram (). GetData (Locker, "List 2"); Console.print line ("start gate data"); Foreach (var x in myList1) {Console.WriteLine ("list1 {0}", x); Forehack (YY in My List 2) {Console.WriteLine ("List 2 {0}", y); }} Console. WrightLine ("and gate data"); Console.ReadLine (); } Public category datagator {Private list & lt; String & gt; _data = new list & lt; String & gt; () {"1", "2", "3", "4", "5"}; Public IEnumerable & lt; String & gt; GetData (object lockObj, string list name) {Console.WriteLine ("{0} starts", list name); Lock (lock objection) {console.light line ("{0} lock leak", list name); Foreign currency (string s_data) {yield returns s; }} Console.WriteLine ("Locked {0} Lock", listName); }}}

results:

2 list 2 3 list 2 4 list 2 5 list 2 list of lock issued 1 list 2 2 list 2 lock list 2 list 2 list 2 2 3 list 2 4 list 2 5 list 2 lock issue list 1 3 list 2 list start 2 lock leak 2 list 2 2 list 2 3 list 2 4 list 2 5 list 2 lock release list 1 4 list 2 list start 2 lock lock List 2 1 list 2 2 list 2 3 list 2 4 list 2 5 list 2 release list lock 1 list 5 list 2 start list 2 lock list 2 1 list 2 2 list 2 3 list 2 4 list 2 5 list 2 lock issue list 1 Lock open Pat GetData

However, he is actually the cool thing result here, note that the line, "GetData Start" after calling DataGetter (). GetData () but everything that happens within the GetData () method is called deferred execution and this yield reflects the beauty and utility of the return statement: anywhere you can exit the loop within your outer loop and There will be no more calls to the inner loop. This means that you do not have to completely run the internal loop again if you do not have it and it also means that you will start getting results in your outer loop.


Comments