Sometimes we write codes that
perform a certain set of functions for many different independent values in
identical way. These set of operations
may take remarkably amount of time.
For example, suppose that the
program needs to calculate the salaries of all employees and write the results
to database. Here is the simplest implementation:
Code:
Since every employee is independent from each other, such a task
can be performed for many employees in parallel way. Here we can utilize
multithreading to shorten the time. We need to implement some steps to achieve
it:
n Decide
how many items will run in parallel. As a best practice, this number should be
the processor count of the machine.
n Divide
the list of items into separated lists. Each of these lists need to have number
of parallel items at maximum.
n In
a loop, run all items of executing list in parallel and synchronize them.
n When
all items of executing list completes, skip to next list in the loop.
Here is the C# Code:
Note that, WaitHandle.WaitAll function cannot synchronize more than 64 synchronization objects. System.NotSupportedException is thrown by .NET Framework.
No comments:
Post a Comment