Poco::ThreadPool提供线程池功能,减少线程的创建和销毁所带来的开销,适合在服务器上应用。创建线程池时指定最少运行线程数和线程池的最大容量,若不指定则采用默认值,取2和16 。线程池的实现机制:有一部分线程始终处于运行状态,但阻塞在Event的wait调用上,所以处于休眠状态,开销并不大。如果我们需要一个线程来运行一段代码(在Poco中,用Runnable的子类表示一个target),则从线程池中去除一个线程,并将这段代码赋给它,并触发Event。然后线程就继续运行了。
活动图
demo
void ThreadPoolTest::testThreadPool()
{
ThreadPool pool(2, 3, 3);
assert (pool.allocated() == 2);
assert (pool.used() == 0);
assert (pool.capacity() == 3);
assert (pool.available() == 3);
pool.addCapacity(1);
assert (pool.allocated() == 2);
assert (pool.used() == 0);
assert (pool.capacity() == 4);
assert (pool.available() == 4);
RunnableAdapter<ThreadPoolTest> ra(*this, &ThreadPoolTest::hello);
pool.start(ra);
assert (pool.allocated() == 2);
assert (pool.used() == 1);
assert (pool.capacity() == 4);
assert (pool.available() == 3);
pool.start(ra);
assert (pool.allocated() == 2);
assert (pool.used() == 2);
assert (pool.capacity() == 4);
assert (pool.available() == 2);
pool.start(ra);
assert (pool.allocated() == 3);
assert (pool.used() == 3);
assert (pool.capacity() == 4);
assert (pool.available() == 1);
pool.start(ra);
assert (pool.allocated() == 4);
assert (pool.used() == 4);
assert (pool.capacity() == 4);
assert (pool.available() == 0);
}
线程池的内存策略有点像MemoryPool,请参加
http://blog.csdn.net/fym0121/article/details/7840801
更多详细信息请查看
java教程网 http://www.itchm.com/forum-59-1.html
分享到:
相关推荐
args) 这是将作业添加到ThreadPool 。 接受要排队的可调用对象( func )以及参数( args... )。 返回模板Func的返回类型的std::future ,以获取返回值。 完整签名: template < typename> auto add (Func&& func,...
线程池 ... ThreadPool< 5> threadPool; // Pass With Thread Pool Size(Worker Count) int a = 5 ; // Push Tasks std::future< int> future1 = threadPool. AddTask (Function1, std::ref (a));
这是C ++中的简单ThreadPool实现。 此实现提供以下功能: 每个池实例的可配置线程数。 动态线程数修改SetThreadCount(size_t) 两种ThreadPool停止模式:同步和异步 main.cpp提供了使用此ThreadPool的示例代码。 ...
local threadpool = require ' threadpool_ext ' threadpool. init ({ logger = { warn = print, error = print, debug = print }, growing_thread_num = 10 , time_service = epoll, }) local env_seq = ...
Linux中的ThreadPool线程池 运行:gcc main.c -o main -lpthread -lrt
线程池 用C ++ 11和Folly实现的简单线程池(facebook C ++基础库)
threadpool实现方式 threadpool实现方式 threadpool实现方式
适用于iOS的线程池库 自1.2版以来,Lightstreamer的iOS客户端库使用的线程池和URL调度库。 这个库做什么 这段代码最初是在2012年编写的,旨在解决iOS SDK和运行时的特定问题: iOS上的NSURLConnection到同一端点的...
rust-threadpool:一个非常简单的线程池,用于并行任务执行
> threadpool::get(TaskFuncPtr& task);读取任务队列中的任务 > threadpool::run();通过get()读取任务并执行 > threadpool.start(); 启动线程池,并通过run()执行任务 > threadpool.exec();封装任务到任务队列中 ...
Thread线程和ThreadPool线程池 Thread:我们可以开启一个线程。但是请大家记住:线程开启会在空间和时间上...ThreadPool:会根据你的CPU的核心数开启一个最合适的线程数量。如果你操作中,非常耗时,就不要用线程池。
threadPool的实现代码
带有相机预览的ThreadPool 在上下载演示应用程序。 该项目演示了如何使用HandlerThread和ThreadPool在后台线程上突发捕获来自Android的Camera API的图像。 图像然后是: 从YUV byte[]转换为RGB int[] 。 从RGB ...
与OpenMP相比,threadpool11的性能 这是在树中找到OpenMP演示的结果。 您机器的硬件并发度是8 threadpool11执行花费了22704毫秒。 threadpool11(没有将来)的执行花费了21198毫秒。 openmp执行花费了21663毫秒...
ThreadPool 线程池管理单元 带调用例子
javascript ThreadPool
一个易于使用的C ++ 11线程池。 使用ThreadPool类对自由函数进行排队,并使用std :: for_each()和std :: transform()的并行版本。 可配置为仅用于标头或与库一起使用。 有许多用法示例。
使用Boost库,Threadpool线程池的使用,包括优先级任务处理,普通任务处理!