Is it threadsafe to computer a value before using it inside a Task using a lambda expression. Inside the Task 'param' is used from a different thread. Is it garanteed that the closure variable 'param' is visible on the Tasks thread when the Task starts immediately after it was computed? Or is it necessary to lock the access to 'param'? After param is computed, it shall be considered to be readonly from this point.
Without lock:
int param = a + b; // param shall be considered immutable after it was calculated
Task.Run(() => MethodWithParameter(param));
With Lock:
int param;
lock(obj) param = a + b; // param shall be considered immutable after it was calculated
Task.Run(() => {
int p2;
lock(obj) p2 = param
MethodWithParameter(p2));
}