vb.net barcode generator open source PARALLEL PROGRAMMING in Visual C#

Generator DataMatrix in Visual C# PARALLEL PROGRAMMING

CHAPTER 24 PARALLEL PROGRAMMING
Data Matrix ECC200 Generation In C#
Using Barcode creation for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications.
www.OnBarcode.com
Read Data Matrix 2d Barcode In C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The next kind of outcome is where the value of the shared counter doesn t match the number of items in the collection. The output from this outcome is as follows: Items Processed: 10001 Press enter to finish The difference is usually quite small one or two items but something has definitely gone wrong. The third kind of outcome is an exception, like this: Unhandled Exception: System.AggregateException: One or more errors occurred. --> System.InvalidOperationException: Queue empty. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.Queue`1.Dequeue() at Listing 27.<>c DisplayClass2.<Main>b 0() in C:\Listing 27\Listing 27.cs:line 26 at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout) at System.Threading.Tasks.Task.WaitAll(Task[] tasks) at Listing 27.Main(String[] args) in C: \Listing 27\Listing 27.cs:line 34 Press any key to continue . . . The cause of the second and third outcomes is a race condition; between the time that a Task calls the Count property to check if there are still items in the collection and the time that the Dequeue method is called to get an item, another Task has changed the state of the Queue<int>. You see a different shared counter value because two Tasks called Dequeue and got the same item from the Queue<int>. You get the exception because two Tasks tried to get the last item from the collection, but only one succeeded. Of course, you could use a critical region to protect access to the collection, but that would have a serious impact on performance for a program which makes such intensive use of the shared data. The collection classes in the System.Collections.Generic namespace are designed to be convenient, but they are not suitable for using with Tasks. However, the collection classes in the System.Collections.Concurrent namespace are designed to work with Tasks, although they are not particularly convenient to use. The concurrent collection classes use sophisticated synchronization techniques to avoid race conditions without incurring the performance problems that critical regions incur. Table 25-5 describes the most useful concurrent collection classes.
Create Barcode In Visual C#
Using Barcode generation for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Generating Code 128 Code Set B In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create Code 128 image in .NET framework applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
PDF 417 Creator In C#.NET
Using Barcode printer for .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
Code 39 Extended Drawer In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Code 3 of 9 image in .NET framework applications.
www.OnBarcode.com
Table 25-5. Concurrent Collection Classes
Create GS1-128 In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create EAN / UCC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
Print Identcode In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create Identcode image in Visual Studio .NET applications.
www.OnBarcode.com
Class
Data Matrix Printer In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Encoding ECC200 In None
Using Barcode maker for Font Control to generate, create Data Matrix ECC200 image in Font applications.
www.OnBarcode.com
ConcurrentQueue ConcurrentStack ConcurrentDictionary
Barcode Encoder In None
Using Barcode printer for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
EAN128 Generation In Visual Basic .NET
Using Barcode printer for Visual Studio .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
Description
UPC Code Scanner In .NET Framework
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Printing UCC - 12 In Objective-C
Using Barcode maker for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
Collect items on a first-in, first-out basis Collect items on a first-in, last-out basis Collect name/value pairs
Print Barcode In None
Using Barcode maker for Office Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
Recognize Universal Product Code Version A In Visual C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
You can see that the concurrent classes are replacements for some of the important generic collection classes (you can learn more about the generic collections in 19). Listing 24-28 shows the use of the ConcurrentQueue class to solve the race condition present in Listing 24-27. Listing 24-28. Using a ConcurrentQueue using System; using System.Collections.Concurrent; using System.Threading.Tasks; class Listing 28 { private static object lockObject = new object(); private static int counter = 0; static void Main(string[] args) { // create a queue and fill it with data ConcurrentQueue<int> myQueue = new ConcurrentQueue<int>(); for (int i = 0; i < 10000; i++) { myQueue.Enqueue(i); } // create an array to make tracking the Tasks simpler Task[] taskArray = new Task[10]; // create and start the Tasks for (int i = 0; i < taskArray.Length; i++) { taskArray[i] = Task.Factory.StartNew(() => { while (myQueue.Count > 0) { // take an item from the queue int item; bool gotItem = myQueue.TryDequeue(out item); if (gotItem) { // increment the counter to report that we have taken an item IncrementCounter(); } } }); }
Data Matrix 2d Barcode Decoder In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
QR Code Printer In Objective-C
Using Barcode generation for iPad Control to generate, create QR Code image in iPad applications.
www.OnBarcode.com
CHAPTER 24 PARALLEL PROGRAMMING
EAN-13 Drawer In None
Using Barcode encoder for Font Control to generate, create EAN-13 image in Font applications.
www.OnBarcode.com
Encode Code 128B In Java
Using Barcode creator for BIRT Control to generate, create ANSI/AIM Code 128 image in Eclipse BIRT applications.
www.OnBarcode.com
// wait for all of the Tasks to finish Task.WaitAll(taskArray); // print out the value of the counter Console.WriteLine("Items Processed: {0}", counter); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } private static void IncrementCounter() { lock (lockObject) { counter++; } } } The key statement in Listing 24-28 is shown in bold. The concurrent collection classes implement methods that start with Try, take an out parameter, and return a bool result. The result indicates whether the operation was successful. In the case of the ConcurrentQueue.TryDequeue method, a result of true means that an item was de-queued and its value assigned to the out parameter (you can learn more about out parameters in 9). A result of false indicates that the operation failed and no item was available. In Listing 24-28, a true result leads to calling the IncrementCounter method and a false result leads to the condition of the enclosing while loop being evaluated again. What this means is that some of the requests you make of a concurrent collection will fail if satisfying the request would lead to a race condition. All of the concurrent collection classes take a similar approach, which can take a while to get used to, but when mastered will provide a good balance between performance and avoiding race conditions.
Summary
This chapter explored one of my favorite C# topics parallel programming using the Task Parallel Library (the other is LINQ, in case you were wondering). Parallel programming has remained a niche skill in recent years, despite the emergence of multi-core and multi-processor machines. Once reason for the niche status is the complexity in writing parallel programs, something which I think the TPL does a good job of addressing. Effective parallel programing still requires an investment of time and patience because it requires a new approach to breaking down your problems and working out which data must be shared (and how it will be protected), but it is an investment that pays dividends you can do more work with less equipment and at lower cost.
Copyright © OnBarcode.com . All rights reserved.