Skip to main content

Closure and Captured variables

Here is a sample code. Can you guess what the output would be ?

 
public static class TasksExample { public static void Execute() { Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.Run(() => CalculateResult(i)); } Task.WaitAll(tasks); //ask.Wait(); //Console.WriteLine(tasks[i].Result); Console.WriteLine("Next work..."); } public static int CalculateResult(int i) { Console.WriteLine($"work starting {i}"); Thread.Sleep(10000); Console.WriteLine($"Completed work {i}"); return 100; } }
Pat yourself if your answer was like this
work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 work starting 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Completed work 10 Next work...
so why is this? For loop is iterating nicely but why did all the tasks got value 10? The answer lies in Closure and Captured variable. The scope of i is in for loop. When you pass the variable to an anonymous function, it wont use the value until the function is executed. In this case it is after the for loop has exited and now the value of i is 10. Hence when Task executes CalculateResult, all tasks now will have value 10. Awesome!!!

If you are using Resharper, it will give you a warning "Access to modified closure". I can't explain it better than here https://www.jetbrains.com/help/resharper/AccessToModifiedClosure.html

So the solution to above issue is to copy the value of i to a local variable with the loop as shown here

public static class TasksExample { public static void Execute() { Task[] tasks = new Task[10]; for (int i = 0; i < 10; i++) { var i1 = i; tasks[i] = Task.Run(() => CalculateResult(i1)); } Task.WaitAll(tasks); //ask.Wait(); //Console.WriteLine(tasks[i].Result); Console.WriteLine("Next work..."); } public static int CalculateResult(int i) { Console.WriteLine($"work starting {i}"); Thread.Sleep(10000); Console.WriteLine($"Completed work {i}"); return 100; } }

and now you get the correct result
work starting 1 work starting 0 work starting 2 work starting 3 work starting 4 work starting 5 work starting 6 work starting 7 work starting 8 work starting 9 Completed work 1 Completed work 2 Completed work 3 Completed work 0 Completed work 5 Completed work 4 Completed work 6 Completed work 7 Completed work 8 Completed work 9 Next work...

Comments

Popular posts from this blog

How to detect HTML5 support for a browser?

HTML5 has introduced lots of new cool  tags . Not all the browsers support all tags and also the implementation of these tags may be different for each browser. HTML5 specification defines the functional aspects of these tags and not the implementation. Also the general concensus is that by 2022 all browsers will support all new features of HTML5. Of all the modern browsers, Chrome seems to have implemented most, if not all, featutes of HTML5. IE9 supports few. Firefox sits in between. So as a developer how do you make use of the cool HTML5 features without causing any compatibility issues with existing browsers? Traditionally developers have used User Agent to detect browser type and use the features accordingly. However these days, you can easily change a User Agent by using addons in your browser. So you need a more robust way to detect the features supported by the browser as the same engine of two different versions of a browser might support different features. To detect t

How to deploy Lambda function in AWS from developers machine?

  In previous post , we saw how to setup local development environment and test a Lambda function locally. As a developer, you may want to test the function in AWS environment quickly to make sure your code is working. In this post we will look at how to deploy the function in your AWS account. We will look at CI/CD pipeline to automatically build and deploy Lambda function in next post.  If you followed the instruction in previous post , you will notice there are three files in your solution LambdaEntrypoint.cs - This is the entrypoint for AWS Lambda.  aws-lambda-tools-defaults.json - This is the configuration file used by Lambda tooling. Update this file to add AWS profile you created while configuring AWS CLI and region you want to deploy your code. Note make sure you have this setting in your template as for some reason, it is missing from default template.  "function-runtime" : "netcoreapp3.1" , serverless.template - This is CloudFormation Serverless applicatio

When to implement IDisposable?

In .Net, garbage collector is very good at cleaning up managed resources. Managed resources are object implemented using .Net framework and something that the Garbage collector is aware of. Unmanaged resource is anything that is implemented outside .Net framework and Garbage collector has no information about it. The combination of Destructor and Dispose can be used to release unmanaged resources for e.g open files or database connection. If you are not using any unmanaged resource, there is no need to implement IDisposable. Garbage collector will take care of release the unused objects and freeing up the memory.