简体   繁体   English

如何将完成端口操作(I/O 绑定函数)排队到 CLR 线程池

[英]How to queue a completion port action (I/O bound function) to CLR ThreadPool

I have an external library that does long running I/O.我有一个执行长时间运行 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.我希望创建一个多线程应用程序,它将使用 ThreadPool 来限制并发线程数,并且我希望添加处理这些外部调用的线程作为完成端口线程(I/O 线程)而不是工作线程(以便限制计算绑定线程)完好无损。

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 .您可以按此处所述对 I/O 线程的工作进行排队。

The managed QueueUserWorkItem queues work to the "worker threads" only.托管 QueueUserWorkItem 队列仅对“工作线程”起作用。 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. UnsafeQueueNativeOverlapped 排队到 I/O 线程,对通过 BindHandle 绑定到 ThreadPool 的 kernel 对象执行的异步 I/O 完成也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AsyncWaitHandle.WaitOne阻止CLR线程吗? 或者它是否创建了I / O完成端口? - Does AsyncWaitHandle.WaitOne block the CLR thread? Or does it create an I/O completion port? 如何验证我的async / await是否正在使用I / O完成端口? - How can I verify that my async/await is using I/O completion port? 如何确定ThreadPool队列中的项目数 - How Can I determine the number of items in ThreadPool Queue 如何在运行时判断我的进程是否受CPU限制或I / O限制 - How to tell at runtime if my process is CPU bound or I/O bound 为什么CLR ThreadPool工作线程使用LIFO命令来处理本地队列中的任务? - Why does CLR ThreadPool worker thread use LIFO order to process tasks from the local queue? HttpWebRequest和I / O完成端口 - HttpWebRequest and I/O completion ports 如何在C#中将队列任务添加到线程池 - how to add queue tasks to threadpool in C# CLR Max I / O(端口完成)线程确定 - CLR Max I/O (PortCompletion) Thread Determination 在给定 Microsoft.Odata.Edm.IEdmModel 的情况下,如何确定绑定操作(函数或操作)所属的 EntitySet? - How can I determine the EntitySet that an bound Operation (function or action) belongs to given a Microsoft.Odata.Edm.IEdmModel? 在c#中,如何从线程池中检查线程是工作线程还是I / O线程? - In c#, how to check the thread from threadpool is a worker thread or an I/O thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM