.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

.NET and future performance

As newer CPUs with more cores, better resources etc become available, does/will the JIT automatically take advantage of them? Will it also parallelize my code so it runs faster without me doing anything special?
newbie_C#
Friday, April 25, 2008
 
 
Yes and no. The JIT will certainly evolve to produce better native code for whatever new processors come out. As far as taking advantage of addtional cores goes, that's an area where Microsoft is actively performing research.

In general, you're not going to get automatic speedups as the number of cores increase unless you've written your code with that in mind.

You can read about Microsoft's plans for future Multi-core optimizations in .NET here:
http://msdn2.microsoft.com/en-gb/magazine/cc163340.aspx
Mark Bessey Send private email
Friday, April 25, 2008
 
 
It's possible that could happen in the future, but you may also want to learn about multithreading.  A good place to start is the BackgroundWorker class, which makes it pretty easy to create and run a background task.  You can inherit from BackgroundWorker, which makes it particularly easy to customize.
Kyralessa Send private email
Friday, April 25, 2008
 
 
So just to clarify, if I write multithreaded code in .NET using either BackgroundWorker or the thread pool then I can take advantage of multi-core CPU's? My assumption has always been "yes".

The research that MS is currently doing goes beyond multithreading and looks at parallelization of code that normally runs in a single thread right?
uggh
Friday, April 25, 2008
 
 
As far as I know, if you use BackgroundWorker, your code should be using the second CPU.  But hopefully someone more experienced in multithreading will step in and comment.  I'm relatively new to it.

You might also want to read this article, to start gaining an understanding of the sorts of code that can be multithreaded:

http://www.joelonsoftware.com/items/2006/08/01.html
Kyralessa Send private email
Friday, April 25, 2008
 
 
If you use BackgroundWorker, that code will run in a separate thread. It may or may not use the second core, at the discretion of the OS thread scheduler.

The research into parallelism exists because raw multithreaded programming is HARD, and getting harder. Anything that can improve the tooling and automate stuff, the better.

As an example I ran into not too long ago, take a look at this code that was intended to be thread safe:

Dictionary<string, Type> typeMap = new Dictionary<string, Type>();
object mapLock = new object();

Type GetMappedType(string name)
{
  if(!typeMap.ContainsKey(name))
  {
    lock(mapLock)
    {
      if(!typeMap.ContainsKey(name))
      {
        typeMap[name] = DoSomethingSlow(name);
      }
    }
  }
  return typeMap[name];
}

That looks pretty thread safe, doesn't it? There's a lock when writing, but reads should be fine, right?

Turns out it's wrong. The problem here is that the Dictionary class is a hash table, and inserts can cause the contents of the table to move to different buckets. Get the wrong mix of readers and writers and your reader would look up the bucket, then the contents of the table would change, and then read from the old bucket.

One fix would be to lock for reading and writing, but then you've just lost any real benefit from multithreading, since you're serializing access and thread contentions will kill you.

This kind of problem is all over the place in multithreaded programming. These kind of bugs won't show up in years of testing, but will fail about five minutes after you put it into production.
Chris Tavares Send private email
Sunday, April 27, 2008
 
 
Anyone that's looking to do multi-core development in .Net should check out Parallel Extensions for .Net. This framework includes PLINQ, which is a LINQ to Objects engine that works across cores. It also includes a runtime thread manager that can do work stealing.
 
There are some natural language constructs you can leverage like:
 
Parallel.For
Parallel.ForEach
Parallel.Invoke
 
 
Here are two good video overviews:
http://channel9.msdn.com/ShowPost.aspx?PostID=361088
http://channel9.msdn.com/Showpost.aspx?postid=375042
 
And you can download the bits from here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=e848dc1d-5be3-4941-8705-024bc7f180ba&displaylang=en
Jared Bienz [MSFT] Send private email
Friday, May 02, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz