13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LINQ beyond .NET 3.5349All of the LINQ providers we’ve seen so far have acted on a particular data source,and performed the appropriate transformations. Our next topic is slightly different—but it’s one I’m particularly excited about.PARALLEL LINQ (PLINQ)Ten years ago, the idea of even fairly low-to-middl<strong>in</strong>g laptops hav<strong>in</strong>g dual-processorcores would have seemed ridiculous. Today, that’s taken for granted—and if the chipmanufacturers’ plans are anyth<strong>in</strong>g to go by, that’s only the start. Of course, it’s only usefulto have more than one processor core if you’ve got tasks you can run <strong>in</strong> parallel.Parallel LINQ, or PLINQ for short, is a project with one “simple” goal: to execute LINQto Objects queries <strong>in</strong> parallel, realiz<strong>in</strong>g the benefits of multithread<strong>in</strong>g with as few headachesas possible. At the time of this writ<strong>in</strong>g, PLINQ is targeted to be released as part ofParallel Extensions, the next generation of .NET concurrency support. The sample Idescribe is based on the December 2007 Community Technology Preview (CTP).Us<strong>in</strong>g PLINQ is simple, if (and only if) you have to perform the same task on eachelement <strong>in</strong> a sequence, and those tasks are <strong>in</strong>dependent. If you need the result ofone calculation step <strong>in</strong> order to f<strong>in</strong>d the next, PLINQ is not for you—but many CPU<strong>in</strong>tensivetasks can <strong>in</strong> fact be done <strong>in</strong> parallel. To tell the compiler to use PLINQ, youjust need to call AsParallel (an extension method on IEnumerable) on your datasource, and let PLINQ handle the thread<strong>in</strong>g. As with IQueryable, the magic is justnormal compiler method resolution: AsParallel returns an IParallelEnumerable,and the ParallelEnumerable class provides static methods to handle the standardquery operators.List<strong>in</strong>g 12.22 demonstrates PLINQ <strong>in</strong> an entirely artificial way, putt<strong>in</strong>g threads tosleep for random periods <strong>in</strong>stead of actually hitt<strong>in</strong>g the processor hard.List<strong>in</strong>g 12.22Execut<strong>in</strong>g a LINQ query on multiple threads with Parallel LINQstatic <strong>in</strong>t Obta<strong>in</strong>LengthSlowly(str<strong>in</strong>g name){Thread.Sleep(StaticRandom.Next(10000));return name.Length;}...str<strong>in</strong>g[] names = {"Jon", "Holly", "Tom", "Rob<strong>in</strong>", "William"};var query = from name <strong>in</strong> names.AsParallel(3)select Obta<strong>in</strong>LengthSlowly(name);foreach (<strong>in</strong>t length <strong>in</strong> query){Console.WriteL<strong>in</strong>e(length);}List<strong>in</strong>g 12.22 will pr<strong>in</strong>t out the length of each name. We’re us<strong>in</strong>g a random 11 sleep tosimulate do<strong>in</strong>g some real work with<strong>in</strong> the call to Obta<strong>in</strong>LengthSlowly. Without the11 The StaticRandom class used for this is merely a thread-safe wrapper of static methods around a normalRandom class. It’s part of my miscellaneous utility library.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!