mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-22 09:01:19 +03:00
v2.0
SmartThreadPool v2.0
This commit is contained in:
@@ -6,42 +6,29 @@ namespace Examples
|
||||
{
|
||||
public class CatchExceptionExample
|
||||
{
|
||||
private class DivArgs
|
||||
{
|
||||
public int x;
|
||||
public int y;
|
||||
}
|
||||
public void DoWork()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
public void DoWork()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<double, double, double>(DoDiv), 10.0, 0.0);
|
||||
|
||||
DivArgs divArgs = new DivArgs();
|
||||
divArgs.x = 10;
|
||||
divArgs.y = 0;
|
||||
try
|
||||
{
|
||||
double result = wir.Result;
|
||||
}
|
||||
// Catch the exception that Result threw
|
||||
catch (WorkItemResultException e)
|
||||
{
|
||||
// Dump the inner exception which DoDiv threw
|
||||
Debug.WriteLine(e.InnerException);
|
||||
}
|
||||
|
||||
IWorkItemResult wir =
|
||||
smartThreadPool.QueueWorkItem(new
|
||||
WorkItemCallback(this.DoDiv), divArgs);
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
int result = (int)wir.Result;
|
||||
}
|
||||
// Catch the exception that Result threw
|
||||
catch (WorkItemResultException e)
|
||||
{
|
||||
// Dump the inner exception which DoDiv threw
|
||||
Debug.WriteLine(e.InnerException);
|
||||
}
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
|
||||
private object DoDiv(object state)
|
||||
{
|
||||
DivArgs divArgs = (DivArgs)state;
|
||||
return (divArgs.x / divArgs.y);
|
||||
}
|
||||
}
|
||||
private double DoDiv(double x, double y)
|
||||
{
|
||||
return x / y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Threading;
|
||||
using Amib.Threading;
|
||||
|
||||
namespace Examples
|
||||
{
|
||||
public class CooperativeCancelExample
|
||||
{
|
||||
public void DoWork(object state)
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
// Queue the work item
|
||||
IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork);
|
||||
|
||||
// Give the work item some time to complete.
|
||||
Thread.Sleep(1000);
|
||||
|
||||
// If the work item hasn't completed yet then cancel it.
|
||||
if (!wir.IsCompleted)
|
||||
{
|
||||
wir.Cancel();
|
||||
}
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
|
||||
// Do some lengthy work
|
||||
private void DoRealWork()
|
||||
{
|
||||
// Do something here.
|
||||
|
||||
// Sample SmartThreadPool.IsWorkItemCanceled
|
||||
if (SmartThreadPool.IsWorkItemCanceled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Sample the SmartThreadPool.IsWorkItemCanceled in a loop
|
||||
while (!SmartThreadPool.IsWorkItemCanceled)
|
||||
{
|
||||
// Do some real work here
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using Amib.Threading;
|
||||
|
||||
namespace STPExamples
|
||||
{
|
||||
/// <summary>
|
||||
/// This is a Parallel QuickSort example.
|
||||
/// It shows how to use the QueueWorkItem with Action<T> methods.
|
||||
/// </summary>
|
||||
public class ParallelQuickSort
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
// Generate an array
|
||||
int[] array = GenerateArray(100);
|
||||
|
||||
// Create a Smart Thread Pool
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
// Use the Smart Thread Pool to parallel the sort
|
||||
QuickSort(stp, array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// QuickSort array using wig to parallel the sort
|
||||
/// </summary>
|
||||
/// <param name="wig">A IWorkItemsGroup to use to parallel the sort</param>
|
||||
/// <param name="array">The array of items to sort</param>
|
||||
public static void QuickSort(IWorkItemsGroup wig, int[] array)
|
||||
{
|
||||
// Initiate the QuickSort
|
||||
wig.QueueWorkItem(QuickSort, wig, array, 0, array.Length - 1);
|
||||
|
||||
// Wait for the sort to complete.
|
||||
wig.WaitForIdle();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sort a subarray in array, starting with the left item and ending in the right item.
|
||||
/// The method uses the IWorkItemsGroup wig to parallel the sort.
|
||||
/// </summary>
|
||||
/// <param name="wig">A IWorkItemsGroup to use to parallel the sort</param>
|
||||
/// <param name="array">The array of items to sort</param>
|
||||
/// <param name="left">The left index in the subarray</param>
|
||||
/// <param name="right">The right index in the subarray</param>
|
||||
private static void QuickSort(IWorkItemsGroup wig, int[] array, int left, int right)
|
||||
{
|
||||
if (right > left)
|
||||
{
|
||||
int pivotIndex = left;
|
||||
int pivotNewIndex = Partition(array, left, right, pivotIndex);
|
||||
|
||||
wig.QueueWorkItem(QuickSort, wig, array, left, pivotNewIndex - 1);
|
||||
wig.QueueWorkItem(QuickSort, wig, array, pivotNewIndex + 1, right);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Partition a subarray of array
|
||||
/// (This is part of the QuickSort algorithm)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static int Partition(int[] array, int left, int right, int pivotIndex)
|
||||
{
|
||||
int pivotValue = array[pivotIndex];
|
||||
Swap(array, pivotIndex, right);
|
||||
int storeIndex = left;
|
||||
|
||||
for (int i = left; i < right; i++)
|
||||
{
|
||||
if (array[i] <= pivotValue)
|
||||
{
|
||||
Swap(array, i, storeIndex);
|
||||
++storeIndex;
|
||||
}
|
||||
}
|
||||
Swap(array, storeIndex, right);
|
||||
return storeIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swap between two item in the array
|
||||
/// </summary>
|
||||
/// <param name="array">Array of integers</param>
|
||||
/// <param name="index1">First index</param>
|
||||
/// <param name="index2">Second index</param>
|
||||
private static void Swap(int[] array, int index1, int index2)
|
||||
{
|
||||
int temp = array[index1];
|
||||
array[index1] = array[index2];
|
||||
array[index2] = temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate an array with the numbers 0-count ordered in reverse
|
||||
/// </summary>
|
||||
/// <param name="count">The number of items in the array</param>
|
||||
/// <returns>Returns the generated array</returns>
|
||||
private static int[] GenerateArray(int count)
|
||||
{
|
||||
int[] array = new int[count];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = array.Length - i;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,11 +16,10 @@
|
||||
<DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>
|
||||
<DefaultTargetSchema>IE50</DefaultTargetSchema>
|
||||
<DelaySign>false</DelaySign>
|
||||
<OutputType>Library</OutputType>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>STPExamples</RootNamespace>
|
||||
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
|
||||
<StartupObject>
|
||||
</StartupObject>
|
||||
<StartupObject>STPExamples.ParallelQuickSort</StartupObject>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>
|
||||
@@ -72,6 +71,16 @@
|
||||
<DebugType>none</DebugType>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseCE|AnyCPU' ">
|
||||
<OutputPath>bin\ReleaseCE\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<BaseAddress>285212672</BaseAddress>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>
|
||||
</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System">
|
||||
<Name>System</Name>
|
||||
@@ -79,6 +88,10 @@
|
||||
<Reference Include="System.Data">
|
||||
<Name>System.Data</Name>
|
||||
</Reference>
|
||||
<Reference Include="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\CompactFramework\2.0\v2.0\WindowsCE\System.Drawing.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml">
|
||||
<Name>System.XML</Name>
|
||||
</Reference>
|
||||
@@ -96,12 +109,14 @@
|
||||
<Compile Include="CatchExceptionExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="CooperativeCancelExample.cs" />
|
||||
<Compile Include="GetExceptionExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="OnWIGIdleEventExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="ParallelQuickSort.cs" />
|
||||
<Compile Include="PriorityExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
@@ -114,6 +129,7 @@
|
||||
<Compile Include="SuspendedWIGStartExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="ThreadEventsExample.cs" />
|
||||
<Compile Include="WaitForAllExample.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
||||
@@ -1,35 +1,34 @@
|
||||
using System;
|
||||
using Amib.Threading;
|
||||
|
||||
namespace Examples
|
||||
{
|
||||
public class SimpleExample
|
||||
{
|
||||
public void DoWork(object state)
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
public void DoWork(int[] numbers)
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
|
||||
// Queue the work item
|
||||
IWorkItemResult wir =
|
||||
smartThreadPool.QueueWorkItem(
|
||||
new WorkItemCallback(this.DoRealWork),
|
||||
state);
|
||||
// Queue the work item
|
||||
IWorkItemResult<double> wir = smartThreadPool.QueueWorkItem(new Func<int[], double>(CalcAverage), numbers);
|
||||
|
||||
// Do some other work here
|
||||
// Do some other work here
|
||||
|
||||
// Get the result of the operation
|
||||
object result = wir.Result;
|
||||
// Get the result of the operation
|
||||
double average = wir.Result;
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
|
||||
// Do the real work
|
||||
private object DoRealWork(object state)
|
||||
{
|
||||
object result = null;
|
||||
// Do the real work
|
||||
private double CalcAverage(int[] numbers)
|
||||
{
|
||||
double average = 0.0;
|
||||
|
||||
// Do the real work here and put the result in 'result'
|
||||
// Do the real work here and put
|
||||
// the result in 'result'
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return average;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Amib.Threading;
|
||||
|
||||
namespace STPExamples
|
||||
{
|
||||
public class MyResource : IDisposable
|
||||
{
|
||||
// ...
|
||||
|
||||
public void DoIt()
|
||||
{
|
||||
//...
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//...
|
||||
}
|
||||
}
|
||||
|
||||
public class ThreadEventsExample
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
stp.OnThreadInitialization += new ThreadInitializationHandler(OnInitialization);
|
||||
stp.OnThreadTermination += new ThreadTerminationHandler(OnTermination);
|
||||
|
||||
stp.QueueWorkItem(DoSomeWork);
|
||||
}
|
||||
|
||||
[ThreadStatic]
|
||||
private static MyResource _resource;
|
||||
|
||||
public static void OnInitialization()
|
||||
{
|
||||
// Initialize the resource
|
||||
_resource = new MyResource();
|
||||
}
|
||||
|
||||
private static object DoSomeWork(object state)
|
||||
{
|
||||
// Use the resouce
|
||||
_resource.DoIt();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void OnTermination()
|
||||
{
|
||||
// Do resource cleanup
|
||||
_resource.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user