.NET 4: Monitor.Enter replaced by Monitor.Enter

Mon, August 31, 2009, 06:27 PM under dotNET
The most common way to protect resources from concurrent access from multiple threads in .NET is by using the C# lock (or SyncLock in VB) statement.
lock(obj)
{
// only 1 thread executes this at a time
}
Under the covers it generates code that is equivalent to:
    Monitor.Enter(obj);
try
{
// only 1 thread executes this at a time
}
finally
{
Monitor.Exit(obj);
}
If you compile the above under Visual Studio 2010 and look at the code generation in your favorite disassembler (e.g. Reflector before it gets a chance to be updated for .NET 4 and hide the detail) you'll find different code generation similar to this:
    bool taken = false;
try
{
Monitor.Enter(obj, ref taken);
// only 1 thread executes this at a time
}
finally
{
if (taken)
{
Monitor.Exit(obj);
}
}
The most important thing to note is the introduction of a new overload for Monitor.Enter (and there is one for TryEnter too) which are public so you can even use them directly.

Instead of explaining here what problem the new APIs are trying to address (asynchronous exceptions occurring just before the try block and after the call to Monitor.Enter), I defer you to Joe's old blog post on orphaned locks.

Don't be too surprised if the original Monitor.Enter overloads (without the boolean parameter) become obsolete.

VS2010 Beta1 demos on parallelism

Thu, August 20, 2009, 11:44 PM under ParallelComputing
Recently I updated the slides of my parallelism session to match the Visual Studio 2010 Beta1 bits.

In this post I'd like to share the demos from that session, also updated for .NET 4 Beta1 (my motto is "better late than never" ;-)

Download my demo code files here at your own risk.

The RayTracer sample can be downloaded along with other parallel extension samples.

HPC Debugging and Profiling support

Fri, August 14, 2009, 12:31 PM under ParallelComputing
Not sure how many subscribers to this blog are HPC developers, but it is an area I will be covering more next year as I am expanding my responsibilities to include the High Performance Computing domain (which obviously falls under the parallelism umbrella). To get a taste of the kind of work we are talking about, read Soma's blog post on the current tooling offerings in that space.

STM.NET

Fri, August 14, 2009, 12:25 PM under ParallelComputing
Hopefully developers on the Microsoft platform subscribe to the announcements on Soma's blog; if you don't, you may have missed this post pointing to the STM.NET dev lab project (this is the 2nd devlab project released from my extended team, the 1st being Axum). If Software Transactional Memory is your interest, stay tuned on the team's blog.

( after seeing their logo, I'll now try to take out of my mind the Atomic tune )

Mobile developer? The Race is On!

Fri, August 14, 2009, 12:17 PM under MobileAndEmbedded
In case my Windows Mobile developer friends have missed this, Loke shares how the Race is On!

Parallel Tasks and Parallel Stacks windows making your debugging life easier

Fri, August 14, 2009, 12:04 PM under ParallelComputing
As I was catching up with my RSS reader I found two posts on John Robbins' blog worth mentioning.

One of them starts with a screenshot and mention of my team's Parallel Stacks window – check out John's blog post on easier multithreaded debugging.

The other blog post offers a macro for the very common requirement to Freeze All Threads But the one of interest. The good news for those of you adopting the task-based programming model is that this is a menu item on the ContextMenu of the new Parallel Tasks window (see last screenshot at the bottom).