这里描述了什么多线程范例?

发布于 2024-08-24 15:19:49 字数 368 浏览 5 评论 0原文

托管线程最佳实践

不要从主程序控制工作线程的执行(例如,使用事件)。相反,设计您的程序,以便工作线程负责等待工作可用,执行它,并在完成时通知程序的其他部分。如果您的工作线程不阻塞,请考虑使用线程池线程。 Monitor..::.PulseAll 在工作线程阻塞的情况下很有用。

我想知道这描述了什么,这样我就可以搜索基本的实现来开始工作。

A snippet from Managed Threading Best Practices on MSDN:

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.

I want to know what this describes so I can search for basic implementations to start working with.

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

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

发布评论

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

评论(3

可是我不能没有你 2024-08-31 15:19:49

该代码片段的含义是:

假设您有一个生产者-消费者问题,并且您有一个消费者线程。如果您有一个专用线程(非 ThreadPool),那么您应该使用 WaitHandle::WaitOne 调用使该线程等待,直到您有要处理的内容(可能在进程队列中)。当生产者填充完队列后,它可以调用 WaitHandle::Set() 来通知您的线程开始处理队列。 WaitOne() 适用于专用线程,因为每次要处理队列时创建一个新的专用线程的成本很高。

DoProcessQueue()
{
    while(RUNTHREAD)
    {
        ... process queue items

        waitHandle.WaitOne(); // will wiat here until producer thread calls Set()
    }
}

建议将 ThreadPool 线程用于小型任务,因此如果您计划使用 WaitOne(),则不应使用 ThreadPool 线程,因为 ThreadPool 是每个应用程序都可用的全局资源,并且阻塞池线程不是一个好的选择。

What the snippet means is that:

Suppose you have a producer-consumer problem for which you have a consumer thread. If you have a dedicated thread(non ThreadPool) then you should use the WaitHandle::WaitOne call to make that thread wait until you have something to process (maybe in a process queue). When the producer is done filling up the queue, it can call WaitHandle::Set() to notify you thread to start processing the queue. WaitOne() is for dedicated threads because it will be expensive to create a new dedicated thread every time you want to process the queue.

DoProcessQueue()
{
    while(RUNTHREAD)
    {
        ... process queue items

        waitHandle.WaitOne(); // will wiat here until producer thread calls Set()
    }
}

ThreadPool threads are recommended for small tasks so if you plan to use WaitOne() then you should not use ThreadPool threads because ThreadPool is a global resource available to every application and blocking the pool threads is not a good option.

献世佛 2024-08-31 15:19:49

听起来像一个工作池。

多个线程等待共享工作队列,获取任务,执行它们,并通过共享同步变量将它们发送到程序的其他部分。

基本语义是 MVar 或其他异步通道的语义。

Sounds like a work pool.

Multiple threads wait on a shared queue of work, taking tasks, executing them, and sending them to other parts of the program via shared synchronization variables.

The basic semantics would be that of an MVar, or other asynchronous channel.

不必你懂 2024-08-31 15:19:49

这听起来像是一种基于分散“代理”的方法。

编写多线程代码的“传统”方式有点像工作中的微观管理 - 老板分发所有工作,其他人都期待着将工作交给他们。任何需要在团队成员之间传递的工作都是通过老板传递的。

基于代理的方法就像授权团队中的每个人寻找并完成工作,根据需要在团队成员之间传递工作。老板知道发生了什么,但只起到执行监督的作用。

This sounds like a decentralised "Agent" based approach.

The "traditional" way of writing multithreaded code is a bit like a micromanagment at work - the boss hands out all work, and everyone else is expected to wait around expectantly for it to be given to them. Any work that needs to be passed between team members is passed via the boss.

An Agent based approach is like empowering everyone on the team to seek out and get the job done, passing work between the team members as required. The boss knows what's going on, but has a executive monitoring role only.

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