如何排队一个完成端口的动作(I / O绑定功能),以CLR线程池 [英] How to queue a completion port action (I/O bound function) to CLR ThreadPool
问题描述
我有没有长时间运行的I / O的外部库。我想创建一个使用线程池来限制并发线程数的和的我想补充的线程交给这些外部调用的完成端口的线程(I / O线程),而不是工作线程(这样一个多线程应用程序该限制的计算绑定线程)是否完好。
我有一个code样品,它省略了外部库,但显示了我已经试过了。
有谁知道该怎么做?或者是它甚至有可能。谢谢
使用系统;
使用的System.Threading;
使用System.Net;
使用的System.Net.Sockets;
使用System.Text;
命名空间ThreadPoolTest
{
类MainApp
{
静态无效的主要()
{
ThreadPool.SetMaxThreads(10,10);
ThreadPool.QueueUserWorkItem(DoWork的); //不工作 - 一个计算绑定线程
ThreadPool.SetMaxThreads(10,10);
//不工作 - 仍然是一个计算绑定线程
((动作<对象>)的DoWork).BeginInvoke(空,回调,空);
Console.Read();
}
静态无效的DoWork(对象o)
{
ShowAvailableThreads();
//调用外部库 - 这确实很长的I / O操作
Thread.sleep代码(10);
}
静态无效的回调(IAsyncResult的AR)
{
ShowAvailableThreads();
}
静态无效ShowAvailableThreads()
{
INT workerThreads,completionPortThreads;
ThreadPool.GetAvailableThreads(出workerThreads,
出completionPortThreads);
Console.WriteLine(WorkerThreads:{0},+
CompletionPortThreads:{1},
workerThreads,completionPortThreads);
}
}
}
您可以如排队工作,为I / O线程<一href="http://blogs.msdn.com/b/ericeil/archive/2008/06/20/windows-i-o-threads-vs-managed-i-o-threads.aspx"相对=nofollow>这里。
在管理QueueUserWorkItem队列 工作的工作线程而已。 UnsafeQueueNativeOverlapped队列 在I / O线程,因为这样做完成为 内核异步I / O执行 已绑定到对象 通过BindHandle线程池。
I have an external library that does long running I/O. I wish to create a multithreaded application that will use ThreadPool to limit simultaneous number of threads and I wish to add threads handing these external calls as completion port thread (I/O threads) rather than worker threads (so that limit for compute bound threads) is intact.
I have a code sample, that omits external library but shows what I've tried already.
Does anyone know how to do that? Or is it even possible. Thank you
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ThreadPoolTest
{
class MainApp
{
static void Main()
{
ThreadPool.SetMaxThreads(10, 10);
ThreadPool.QueueUserWorkItem(DoWork); //doesn't work - a compute-bound thread
ThreadPool.SetMaxThreads(10, 10);
//doesn't work - still a compute-bound thread
((Action<object>)DoWork).BeginInvoke(null, Callback, null);
Console.Read();
}
static void DoWork(object o)
{
ShowAvailableThreads();
//call to external library - that does a long I/O operation
Thread.Sleep(10);
}
static void Callback(IAsyncResult ar)
{
ShowAvailableThreads();
}
static void ShowAvailableThreads()
{
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads,
out completionPortThreads);
Console.WriteLine("WorkerThreads: {0}," +
" CompletionPortThreads: {1}",
workerThreads, completionPortThreads);
}
}
}
You can queue work for the I/O threads as described here.
The managed QueueUserWorkItem queues work to the "worker threads" only. UnsafeQueueNativeOverlapped queues to the I/O threads, as do completions for asynchronous I/O performed on kernel objects that have been bound to the ThreadPool via BindHandle.
这篇关于如何排队一个完成端口的动作(I / O绑定功能),以CLR线程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!