`
javababy1
  • 浏览: 1171769 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Smart Thread Pool 介绍

阅读更多

By Ami Bar. A smart Thread Pool implementation in .NET.
http://www.codeproject.com/cs/threads/smartthreadpool.asp

顾名思义,智能线程池.一定比自带的线程池有过人之处.不然也没有必要再搞个出来了.

  • 可创建线程池实例。
  • 可动态调整线程池工作线程数量。
  • WorkItem 可以返回信息。
  • 未执行 WorkItem 可被取消。
  • WorkItem 执行时可使用调用者上下文。
  • 调用者可等待多个或全部 WorkItem 执行结束。
  • WorkItem 允许拥有一个执行结束时被执行的 PostExecute 回调委托。
  • 可以向 WorkItem 传递一个状态对象,并且会在执行结束时自动调用 IDisposable.Dispose()。
  • WorkItem 异常会传递给调用者。
  • 支持 WorkItem 分组。
  • 可挂起线程池或分组。
  • 可以设置 WorkItem 优先级。
  • 可以设置线程优先级。
相比较而言,System.Threading.ThreadPool 只适合做些简单的工作。(本来也是为了异步委托而实现的)

1. 简单示例
static void Main(string[] args)
{
SmartThreadPool smart = new SmartThreadPool();
smart.Start();

IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
Console.WriteLine("Thread:{0}; State:{1}", Thread.CurrentThread.ManagedThreadId, state);
return DateTime.Now;
}, 123);

SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);

smart.Shutdown();
}

输出:
Thread:11; State:123
2007-8-9 12:42:51

2. 参数设置
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;
stp.CallToPostExecute = CallToPostExecute.Always;
stp.ThreadPriority = ThreadPriority.Highest;
stp.UseCallerCallContext = true;
stp.MaxWorkerThreads = 20;

SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();

IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null);

smart.WaitForIdle();
smart.Shutdown();

3. 自动释放 State

如果 State 对象实现了 IDisposable 接口,那么我们可以通过设置线程池参数,当 WorkItem 结束时自动调用 state.Dispose()。
class State : IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose...");
}
}

public class Program
{
static void Main(string[] args)
{
STPStartInfo stp = new STPStartInfo();
stp.DisposeOfStateObjects = true;

SmartThreadPool smart = new SmartThreadPool(stp);
smart.Start();

IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, new State());

SmartThreadPool.WaitAll(new IWorkItemResult[] { result });
Console.WriteLine(result.Result);

smart.Shutdown();
}
}

输出:
Dispose...
2007-8-9 12:43:21

4. PostExecute CallBack

该委托在 WorkItem 结束时被调用。
SmartThreadPool smart = new SmartThreadPool();
smart.Start();

IWorkItemResult result = smart.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});

5. Work Items Group

根据使用目的不同,我们可以将线程池划分为多个组。针对组进行管理,显然要比创建多个线程池对象要好些。
SmartThreadPool smart = new SmartThreadPool();
smart.Start();

IWorkItemsGroup group = smart.CreateWorkItemsGroup(3);
IWorkItemResult result = group.QueueWorkItem(delegate(object state)
{
return DateTime.Now;
}, null, delegate(IWorkItemResult _result)
{
Console.WriteLine(_result.Result);
});

group.WaitForIdle();
smart.Shutdown();

更多细节可参考作者原文档。
附:作者是个有心人,这个组件和文档一直持续升级和维护,在此表示感谢。 [yes]
分享到:
评论

相关推荐

    SmartThreadPool

    Smart Thread Pool is a thread pool written in C#. The implementation was first based on Stephan Toub's thread pool with some extra features, but now it is far beyond the original. Here is a list of ...

    Smart Thread Pool-开源

    智能线程池是在纯.net中实现的丰富线程池,可以在Windows,WindowsCE,Silverlight,ASP.NET和Mono上运行。

    Smart Thread Pool

    关于线程池的控制,让你对多线程的认识提升到另一高度。。

    .Net 高性能线程池组件 smartthreadpool

    C#写的高性能线程池开源组件,比framework自带的要好用多了.

    Boost C++扩展库

    Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之...Thread 可移植的C++多线程库;Python 把C++类和函数映射到Python之中;Pool 内存池管理;smart_ptr 智能指针。

    C++标准库介绍.pdf

    Thread 可移植C多线程库 Python 把C类和映射到Python的中 Pool 内存池管理 smart_ptr 5个智能指针学习智能指针必读份不错参考是来自CUJ文章: Smart Poers in Boost,哦这篇文章可以查到CUJ是提供在线浏览中文版见笔者...

    boost 1.41 中文文档,使用帮助,教程手册

    fatalerror99 array, bind & mem_fn, dynamic_bitset, function, functional/hash, in_place_factory & typed_in_place_factory, lambda, ref, smart_ptr, static_assert, string_algo, type_traits, typeof ...

    Mastering the C++17 .pdf

    thread pool using std::future . I'll teach concepts beyond what you'd find in a reference manual. You'll learn the difference between monomorphic, polymorphic, and generic algorithms (Chapter 1 , ...

    Boost库定制安装更新版

     Thread  可移植的C++多线程库  Python  把C++类和函数映射到Python之中  Pool  内存池管理  smart_ptr  5个智能指针,学习智能指针必读,一份不错的参考是来自CUJ的文章:  Smart Pointers in Boost,哦,...

    cpp-diary:杂项和练习用C ++编写的程序

    C ++日记我编写了一些C ++程序供以后参考。

Global site tag (gtag.js) - Google Analytics