.NET 4.0中ThreadPool是如何实现的?

发布于 2025-01-04 12:43:09 字数 241 浏览 1 评论 0原文

我最近尝试弄清楚 ThreadPool 类的解决方案如何在 .NET 4.0 中工作。我尝试阅读反映的代码,但它对我来说似乎有点太广泛了。

有人可以简单地解释一下这个类是如何工作的,即

  1. 它如何存储传入的每个方法
  2. 它是线程安全的吗,据说多个线程试图将它们的方法排入线程池中?
  3. 当它达到可用线程的限制时,当其中一个线程空闲时,它如何返回执行队列中等待的剩余批处理?有回调机制吗?

I recently tried to work out how the solution to a ThreadPool class works in .NET 4.0. I tried to read through a reflected code but it seems a bit too extensive for me.

Could someone explain in simple terms how this class works i.e.

  1. How it stores each methods that are coming in
  2. Is it thread safe, supposedly multiple threads try to enqueue their methods in the thread pool?
  3. When it reaches the limit of available threads, how does it return to execute the remaining batch waiting in the queue when one of the threads becomes free? Is there some callback mechanism for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2025-01-11 12:43:09

当然,在没有实际实现的情况下(或者在没有 Eric Lippert :) 的情况下,我所说的只是常识:

  1. 线程池拥有一个内部(循环?)队列,其中保存任务(因此 <代码>QueueUserWorkItem)。
  2. 将任务放入队列中是线程安全的(这是肯定的,因为我自己已经在这种情况下使用过多次)。
  3. 我认为每个线程无限循环,并在完成当前任务时自动从队列中获取任务(当然以线程安全的方式)。如果队列为空,它就会阻塞。

Of course, in the absence of the actual implementation (or in the absence of Eric Lippert :) ) what I'm saying is only common sense:

  1. The thread pool holds an internal (circular?) queue where the tasks are kept (hence QueueUserWorkItem).
  2. Putting tasks in the queue is thread-safe (this is for sure, as I've used myself in this scenario several times).
  3. I think that each thread loops indefinitely and keeps taking tasks from the queue (in a thread-safe manner of course) automatically when it's done with the current task. If the queue is empty it will just block.
冷夜 2025-01-11 12:43:09
  1. 在委托队列中

  2. TBH,我不确定,但如果不是,它是危险的,几乎没用,可能是 M$ 发出的最糟糕的代码,(甚至包括 Windows ME)。假设它是线程安全的。

  3. 工作线程是 while 循环,在工作请求队列上等待委托,在可用时调用一个委托,然后在委托返回时再次循环以再次在队列上等待另一个委托。不需要任何回调。

  1. In a queue of delegates

  2. TBH, I don't know for sure but, if it's not, it's dangerous, nearly useless and probably the worst code ever emitted by M$, (even including Windows ME). Just assume it's thread safe.

  3. The work threads are while loops, waiting on the work request queue for a delegate, invoking one when it becomes available, then looping back round again when the the delegate returns to wait on the queue again for another delegate. There is no need for any callback.

触ぅ动初心 2025-01-11 12:43:09
  1. 我不太清楚,但在我看来,它将它存储在一个集合中
    任务
  2. MSDN 说是

  3. GetMaxThreads() 返回一次性执行线程的数量,如果
    当你到达这个边界时,所有其他人都在排队。据我了解你
    需要了解线程何时执行的机制。有
    RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean)

  1. I don't know exectly but to my mind it stores it in a collection of
    Task
  2. MSDN says yes

  3. GetMaxThreads() returns the amount of onetime-executed threads if
    you reach this border all others are queued. As I understand you
    need mechanism for knowing when thread is executed. There is
    RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文