Programming in the Age of Concurrency - Anders Hejlsberg and Joe Duffy: Concurrent Programming with
Oct 13, 2007 at 11:26 AMGood video.
littleguru wrote:I would like to know how you guys make sure that function a(i) isn't calling a x(...) that relies on a global variable (like a singleton).
That's an interesting thought. I have no particular insight into the answer, but I'm guessing it would be quite difficult to implement due to at least a couple reasons:
a) To generalize your question (correctly, I hope), it's not just globals that are a concern, it's any shared state--is your lambda expression using shared state in a thread-safe fashion? Here's a simple non-global example:
R r = new R();
Parallel.For(0, 1000,
i => {
int v = r.Next(1, 6);
...
});
Here we have N threads accessing r. How do you determine whether r is being used in a thread-safe manner? If you know that class R is not thread-safe, then this code isn't either. But how would a compiler or runtime determine whether this is safe, through static or run-time analysis of all the possible code paths? This seems to me to be a rather difficult problem.
b) Aside from the difficultly of analyzing the code, how do you define what's "correct?" If I substitute a thread-safe class for R how would you now determine that the use of r is now thread-safe? Perhaps analysis would show the use of a locking mechanism?
What if the new class is written using a lockless technique, how would you determine whether it's safe or not?
As mentioned in the video some problems require shared state and some problems don't. For example, consider applying update operations to a graph in parallel. Shared state isn't a side effect here; in fact, the whole point is to update shared state. How would
analysis take that into account?
It's an interesting thought, though. ![]()