C线程池效率
可能的重复:
多少个线程太多?
我有一个很大的用于< /code> 循环,我希望将每个项目传递给线程上的函数。我有一个一定大小的线程池,我想重用线程。做到这一点最有效的方法是什么?
线程池最有效的大小是多少?或者至少解决这个问题的最佳方法是什么?
pthread_t thread_id[10];
int i;
for(i = 0; i < 10000000; i++) {
pthread_create(thread_id[?], NULL, threadFunc, params[i]);
}
Possible Duplicate:
How many threads is too many?
I have a large for
loop, in which I want each item to be passed to a function on a thread. I have a thread pool of a certain size, and I want to reuse the threads. What is the most efficient way to do this?
What would be the most efficient size for the thread pool? Or at least what is the best way to figure it out?
pthread_t thread_id[10];
int i;
for(i = 0; i < 10000000; i++) {
pthread_create(thread_id[?], NULL, threadFunc, params[i]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
线程太少意味着浪费性能机会,太多线程会浪费每个线程消耗的资源,并且会因线程之间的争用而降低性能。我会使用测量值来看看什么数字效果最好。只是好奇,您是否有理由希望每次迭代都是一个新线程?
Too few threads means wasted performance opportunity and too many threads wastes resources consumed by each thread and can reduce performance due to contention among threads. I would use measurements and see what number works the best. Just curious, is there are reason why you want each iteration to be a new thread?
如果您没有阻止线程执行磁盘或网络 I/O,则池中最有效的线程数是每个处理器一个(假设所有工作都在线程池线程上完成)。其余的工作项目应排队并在完成后拉出。
If you aren't blocking the threads to do disk or network I/O then the most efficient number of threads in the pool is one per-processor (assuming all the work is being done on the thread pool threads). The rest of the work items should be queued up and pulled off as they are completed.
如果您想要的只是通过独立迭代并行化大型
for
循环,也许您可以使用 OpenMP。您可以选择不同的
schedule
选项和其他参数来调整行为,包括线程数(如果需要);默认情况下,OpenMP 将创建与可用硬件上下文一样多的线程,并在所有线程之间按相等比例分配工作。If all you want is to parallelize a large
for
loop with independent iterations, maybe you can use OpenMP.You can select different
schedule
options and other parameters to tune behavior, including the number of threads if you need; by default, OpenMP will create as many threads as there are hardware contexts available, and divide the work in equal proportion between all threads.您可能想了解生产者-消费者模式。简而言之:您首先创建线程,该线程调用一些函数,如果有的话,该函数返回要处理的内容,或者如果没有则阻塞线程。
它可以在 Windows 中通过 CreateUserACP(或 QueueUserACP)或 {Post,Get}QueuedComplentionStatus API 实现。
在 UNIXies 中 - 使用 pthread_cond_* 或 pthread_workqueue_* 调用。
You probably want to look on producer-consumers pattern. In short: you first create your threads, which call some func, which returns things to process if there are any, or just blocks thread if there are none.
It can be implemented in Windows by CreateUserACP (or QueueUserACP) or {Post,Get}QueuedComplentionStatus API's.
In UNIXies - with pthread_cond_* or pthread_workqueue_* calls.