.NET 4.0中ThreadPool是如何实现的?
我最近尝试弄清楚 ThreadPool 类的解决方案如何在 .NET 4.0 中工作。我尝试阅读反映的代码,但它对我来说似乎有点太广泛了。
有人可以简单地解释一下这个类是如何工作的,即
- 它如何存储传入的每个方法
- 它是线程安全的吗,据说多个线程试图将它们的方法排入线程池中?
- 当它达到可用线程的限制时,当其中一个线程空闲时,它如何返回执行队列中等待的剩余批处理?有回调机制吗?
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.
- How it stores each methods that are coming in
- Is it thread safe, supposedly multiple threads try to enqueue their methods in the thread pool?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,在没有实际实现的情况下(或者在没有 Eric Lippert :) 的情况下,我所说的只是常识:
Of course, in the absence of the actual implementation (or in the absence of Eric Lippert :) ) what I'm saying is only common sense:
QueueUserWorkItem
).在委托队列中
TBH,我不确定,但如果不是,它是危险的,几乎没用,可能是 M$ 发出的最糟糕的代码,(甚至包括 Windows ME)。假设它是线程安全的。
工作线程是 while 循环,在工作请求队列上等待委托,在可用时调用一个委托,然后在委托返回时再次循环以再次在队列上等待另一个委托。不需要任何回调。
In a queue of delegates
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.
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.
任务
MSDN 说是
GetMaxThreads() 返回一次性执行线程的数量,如果
当你到达这个边界时,所有其他人都在排队。据我了解你
需要了解线程何时执行的机制。有
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean)
Task
MSDN says yes
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)