Ale Contenti and Louis Lafreniere: Understanding Exceptions and When/How to Handle Them

Good 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).
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.
I assume there is a reason why you have to explicitly say you want your code parallelized. It's still an imperative world with islands of declarativeness. But I assume that in the future the compiler or CLR will auto-parallelize some code, where it can determine the safety of it.
But the team has its priorities right. It first creates the foundation for parallelism. Then later it can start to think about (or someone else can) the cases that are safe to parallelize - and where it makes sense to do so.
Maybe, with all the threads running on the machine, it wouldn't make sense to parallize some tasks, even though it would be possible to do so.
Just a thought...
One thing is for sure. The C#/CLR team has a great understanding of how to maximize impact and value whilst keeping disruption minimal.
One thing I didn't pick up from the video - is the Parallel Task Lib built on top of the existing Thread Pool stuff?
Another great video.
amotif wrote: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?
ktr wrote:parallel for (int i = 0; i < 10; i++)
// do something
littleguru wrote:I have some questions. Let's have this simple call:
Parallel.For(0, count, i => a(i));
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). How are you checking that? Isn't that checked at all - is it up to the dev to check that? I can see a lot of people saying: "my code worked and now it isn't working anymore, damn PFX library. It killed it!" instead of checking out there code for possible "side effects".
Are there papers or docs around that you used for your for global variables and shared resources checks?
I liked it when we got into the more "geeky" part... the thing about what item is picked from the queue etc. Would have been nicer if we would have gotten more about that stuff - speaking of the "geeky" stuff.
But it was a very nice video! Thanks for your time guys.
tarlano wrote:Can anyone answer the question of how PFX relates to the Concurrency and Coordination Runtime (CCR) that ships with the MS robotics studio?
Anthony
Now, about the more general issue of shared state. Judah hit the nail right on the head in his response. We do not (currently) reject programs due to reliance on shared state. LINQ tends to lead programmers down a more functional programming style so
the problem is less pervasive (though still there) in PLINQ.
Please take a look at
http://www.bluebytesoftware.com/blog/2007/09/15/ParallelFXMSDNMagArticles.aspx for some more details on what we call "parallelism blockers." This includes shared state, thread affinity, and slight changes in exception behavior. Our story here is not completely
ironed out, at least not ironed out enough to describe to everybody right now. When it is, you can be sure we'll be back here on Channel9 to discuss it.
Thanks for all of the great feedback. Keep it coming.
---joe
Congratulation! I'm very excited about this technology since I saw a link to Joe's blog saying about PLINQ.
One thing I would like to know if there will be on TaskManager a way to inform a dependency between tasks. So, using some kind of topological sorting, the TaskManager would know how to deal with these dependencies.
Dals
Joe's book just went up on Safari. You can purchase the PDF of it now under their early access program:
Concurrent Programming on Windows Vista: Architecture, Principles, and Patterns
by Joe Duffy
Last Updated on Safari: 2007/10/26
Publisher: Addison Wesley Professional
Pub Date: April 25, 2008 (est.)
That's my weekend sorted! Happy Dev!
Hi!
How do you break a Parallel.For
Got this...
System.Threading.
Parallel.For(1, maximumIterations_, 1, dd =>Hi Jaime,
There is an overload of Parallel.For whose 'body' lambda is passed a ParallelState object. This object offers a 'Stop' method which is effectively the same as 'break' in a sequential loop. So for example, your code would look something like (changes highlighted):
System.Threading.Parallel.For(1, maximumIterations_, 1, (dd, state) =>
{
s = trapzd(function, lower, upper, dd);
if (Math.Abs(s - olds) < tolerance_ *
Math.Abs(olds))
{
exeeded = true;
state.Stop();
return;
}
olds = s;
});
I didn't try to compile this, but it should work and do what you are looking for. Take care,
---joe
Hello guys,
Just a thought on shared data.
Would it be safe to say that the data is read only or write only is thread safe? It seems like if I separate the output to a dedicated field, I can merged the result and overwrite(redirect) the source data if necessary. That's just a special case though.
For some other cases, I think it would be much helpful if .Net itself have a build-in data manager to manage shared resources. Something like DB but on the level of dynamic memory. Since .Net already have its own memory manager, that able to track who is referencing
the data, this could be an great add-on.
The way I think of it is that when you pass by reference, you will need the lock. If you pass by value is fine.
You can declare the variable to read-only(not const) and write-only, but they can change the read-write property. Only one thread, the parent/master thread has the full access to update read-only data/read from write only data. With a build-in
strict timer/wait timer/indefinite wait on the fields for the parent thread. Also you can suspend all other thread when the parent thread is using the field / or allow X amount of threads / allow all threads.
Just a thought
magicalclick wrote:Hello guys,
Just a thought on shared data.
Would it be safe to say that the data is read only or write only is thread safe? It seems like if I separate the output to a dedicated field, I can merged the result and overwrite(redirect) the source data if necessary. That's just a special case though.
For some other cases, I think it would be much helpful if .Net itself have a build-in data manager to manage shared resources. Something like DB but on the level of dynamic memory. Since .Net already have its own memory manager, that able to track who is referencing the data, this could be an great add-on.
The way I think of it is that when you pass by reference, you will need the lock. If you pass by value is fine.
You can declare the variable to read-only(not const) and write-only,but they can change the read-write property. Only one thread, the parent/master thread has the full access to update read-only data/read from write only data. With a build-in strict timer/wait timer/indefinite wait on the fields for the parent thread. Also you can suspend all other thread when the parent thread is using the field / or allow X amount of threads / allow all threads.
Just a thought