托管线程一般建议 #3

发布于 2024-07-22 19:16:06 字数 418 浏览 7 评论 0原文

一般性建议#3 中的这句话是什么意思?

不控制worker的执行 主程序中的线程(使用 例如,事件)。 相反,设计 你的程序,以便工作线程 负责等到下班 可用,执行它,并且 通知程序的其他部分 等结束了。 如果你的工作线程 不要阻塞,考虑使用线程 池线程。 Monitor.PulseAll 是 在工人的情况下有用 线程阻塞。

有人可以举例说明吗?

-- 来源:MSDN - 托管线程最佳实践

What does this from General Recommandation #3 mean?

Don't control the execution of worker
threads from your main program (using
events, for example). Instead, design
your program so that worker threads
are responsible for waiting until work
is available, executing it, and
notifying other parts of your program
when finished. If your worker threads
do not block, consider using thread
pool threads. Monitor.PulseAll is
useful in situations where worker
threads block.

Can someone explain by examples please?

-- Source: MSDN - Managed Threading Best Practices

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

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

发布评论

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

评论(3

划一舟意中人 2024-07-29 19:16:07

我认为这个建议也可能是指创建一个线程,然后在其上重复调用诸如 SuspendResume 之类的方法来控制它的运行时间。

通常认为更好的形式是让线程执行一个 while 循环,该循环包含某种锁定原语(例如 ManualResetEvent 或一些类似的原语),以便在出现问题时向线程发出信号。还有更多工作要做。

I think this recommendation may also be referring to creating a thread, and then repeatedly calling methods like Suspend and Resume on it to control when it runs.

It is generally considered better form to have the thread execute a while loop that contains some kind of locking primitive (like a ManualResetEvent or some similar primitive) to signal the thread when there is more work to be done.

飘过的浮云 2024-07-29 19:16:07

我认为这意味着您不应该手动创建工作线程来处理任务(例如保存文件),而是拥有一个适当的系统(或使用 ThreadPool.QueueUserWorkItem),您可以在其中将任务/作业排入队列,并且现有工作人员正在等待任务到达(可能使用监视器 Wait 或 AutoResetEvent)。 这样做意味着您可以重复使用线程,而不必不断创建和销毁它们。

.NET 4.0 有一个新的内置 Task 类具有一堆支持类,使这种编程风格变得更容易,这样您就不必在每个项目中重新发明它。

I take this to mean that you shouldn't manually create worker threads to handle tasks (e.g. save a file), but rather have a system in place (or use ThreadPool.QueueUserWorkItem) where you can enqueue a task/job and an existing worker is waiting for a task to arrive (perhaps using a monitor Wait or an AutoResetEvent). Doing it this way means you can re-use threads rather than having to constantly create and destroy them.

.NET 4.0 has a new built-in Task class with a bunch of supporting classes to make this style of programming easier so that you don't have to re-invent this in each project.

假面具 2024-07-29 19:16:06

好吧,基本上有两种方法可以将工作分配给工作线程。 第一个是将工作项存储在队列中。 当您有工作要做时,您可以将其推入队列并向工作人员发出信号。 您的工作线程看起来像这样:

while( !quit ) {
    WaitForWork();
    GetWorkItem();
    ExecuteWorkItem();
}

根据建议,这是您应该采取的方法。

另一种方法是维护工作人员队列。 当您需要完成工作时,您可以从队列中获取一个工作人员(如果队列为空,则创建一个工作人员)并告诉它运行该项目。 第二种方法更难以编码并且通常效率较低。

Well, there are basically two ways you can dole out work to your worker threads. The first is to store work items in a queue. When you have work to do, you push it onto the queue and signal the workers. Your worker threads would look something like this:

while( !quit ) {
    WaitForWork();
    GetWorkItem();
    ExecuteWorkItem();
}

This is the approach you're supposed to take, according to the recommendation.

The other approach is to maintain a queue of workers. When you have work that you need to do, you grab a worker from the queue (or create one if its empty) and tell it to run the item. This second approach is more difficult to code and is generally less efficient.

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