什么是线程池?

发布于 2024-07-27 22:13:16 字数 61 浏览 8 评论 0原文

实现线程池的概念是什么(在 pthreads 的帮助下用 C 语言)? 如何从线程池中分配一个线程来执行?

What is the concept of implementing Thread-pool (in C with help from pthreads)?
how can a thread be assigned to execute from the thread pool ?

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

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

发布评论

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

评论(3

无悔心 2024-08-03 22:13:16

添加到匿名的答案中,我想提一下,有固定线程池,其中运行的线程数量是固定的; 缓存线程池,可以动态增长,然后在没有可用工作时收缩; 动态线程池还可以受到最大线程数和/或工作队列的最大长度的限制。 我认为这类东西实际上没有一套术语,人们很少遇到用 C 编写的非固定 TP,但至少应该知道固定 TP 并不是唯一的类型。

Adding to anon's answer I'd like to mention that there are Fixed thread pools which have fixed numbers of thread running in them; Cached thread pools which can dynamically grow and then shrink when no work is available; Dynamic thread pools can also be bound by maximum number of threads and/or maximum length of the work queue. I don't think there is actually a set terminology for this kind of stuff and one rarely encounters non-fixed TPs written in C but at least one should know that fixed TP is not the only kind out there.

猥︴琐丶欲为 2024-08-03 22:13:16

线程池是在应用程序启动时创建的固定数量线程的集合。 然后,线程通常通过由信号量控制的队列等待发送给它们的请求。 当发出请求并且至少有一个线程在等待时,该线程将被唤醒,为请求提供服务,然后返回等待信号量。 如果没有可用线程,请求将排队等待有可用线程。

线程池通常是一种比简单地为每个请求启动一个新线程更有效的资源管理方式。 但是,某些体系结构允许在应用程序运行时创建新线程并将其添加到池中,具体取决于请求加载。

A thread-pool is a collection of a fixed number of threads which are created on application startup. The threads then sit waiting for requests to come to them, typically via a queue controlled by a semaphore. When a request is made, and there is at least one thread waiting, the thread is woken up, services the request, and goes back to waiting on the semaphore. If no threads are available, requests queue up until one is.

Thread-pools are a generally more efficient way of managing resources than simply starting a new thread for every request. However, some architectures allow new threads to be created and added to the pool as the application runs, depending on request loading.

野鹿林 2024-08-03 22:13:16

澄清一下之前答案中的一些内容:

实例化越来越多的线程导致效率低下的原因是上下文切换时间。 操作系统定期将处理器上的一个线程切换为另一个线程。 这涉及保存一个线程的状态并从内存加载另一个线程的状态,因此每次上下文切换都需要不可忽略的时间,即 N 毫秒。

例如,如果有 10 个线程,则上下文切换需要 10*N 毫秒。 如果有 1000 个线程,则为 1000*N 毫秒。 随着并发线程数量的增加,最终上下文切换开始压倒多线程带来的效率。 您的应用程序在最佳线程数方面具有最佳点。 一旦通过实验确定了这个最佳数字,您就可以将线程池​​最大大小设置为该线程数,从而从多线程中获得最大效率。

To clarify something in previous answers:

The reason that instantiating more and more threads leads to inefficiency is context switching time. The OS periodically switches one thread for another on the processor. This involves saving one thread's state and loading another thread's state from memory, so it takes non-negligible time, N ms, per context switch.

For example, if you have 10 threads, the context switching takex 10*N ms. If you have 1000 threads, it's 1000*N ms. As the number of concurrent threads increases, eventually the context switching begins to overwhelm any efficiencies derived from multithreading. Your application has a sweet spot in terms of the best number of threads. Once you determine this sweet number by experimentation, you can set your thread pool max size to that number of threads, thereby obtaining maximum efficiency from multithreading.

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