线程创建/销毁开销 [英] Thread Creation/Destruction overhead

查看:459
本文介绍了线程创建/销毁开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个应用程序,其中应用程序假设要处理记录数据(范围为1-500000).我为每个块创建线程,

如果我创建/销毁线程至少11k次,那么....这是开销吗?


提前谢谢.
-普拉夫拉



更新:

我尝试一次创建线程.但这不利地影响了性能.比每次创建线程要花费更多的时间.

这是与线程相关的任何与内存相关的问题吗?

需要帮助

Hi,

I am developing an application where application suppose to process the data of records (ranges from 1 - 500000). For each block i am creating threads,

If i am creating /destructing threads for at least 11k times, then .... is it the overhead ??


Thanks in advance.
-Prafulla



UPDATE :

I tried with creating thread at once. But it is adversaly affecting the performance. It is taking more time than creating threads at each time.

Is ther any thread specific memory related issue ???

NEED HELP

推荐答案

可能.您应该安排一些测试来验证执行时间.您还可以查看线程池模式 [
Probably yes. You should arrange a little test to verify execution times. You may also have a look at Thread Pool Pattern[^].


是的,创建和销毁线程确实有开销,也许更好的选择是有一个管理器线程,然后有一个工作线程,您可以从管理器线程中将工作分配给该工作线程.

因此,您只能在程序开始时创建线程,并等待一条消息,告诉他们要处理哪些记录.
完成后,他们会向管理器线程发送一条消息,告知他们已完成,并希望处理下一组记录.
Yes creating and destroying threads do have an overhead, perhaps the better option is to have one manager thread and then have worker thread to which you assign the work to from the manager thread.

hence you only create threads at the start of the program and have the wait on a message telling them which records to process.
Once done they send a message to the manager thread telling it they are done and want the next set of records to process.


非常不合理的开销.您应该完全重新考虑您的设计.使用线程可能有不同的原因.您是否需要更充分地使用您的CPU/内核(但是您几乎不需要使用比内核更多的CPU)还是线程代表了应用程序的逻辑实体(例如在著名的餐饮哲学家问题中).

您很有可能可以使用固定数量的线程之一.您还可以使用线程池模式,请参见
http://en.wikipedia.org/wiki/Thread_pool_pattern [ ^ ].

—SA
Very unreasonable overhead. You should re-think your design totally. There are could be different reasons for using threads. Do you need to more fully use your CPUs/cores (but then you hardly need more CPUs than cores) or thread represent logical entities of your application (such as in the famous problem of dining philosophers).

It is very likely that you can use one of a fixed small number of threads. You can also use the Thread Pool pattern, see http://en.wikipedia.org/wiki/Thread_pool_pattern[^].

—SA


这篇关于线程创建/销毁开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆