创建线程时有多少开销? [英] How much overhead is there when creating a thread?

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

问题描述

我刚刚审查了一些真正可怕的代码 - 通过创建一个新的线程来打包和组装消息在一个新的线程为每个单个消息发送串口发送消息的代码。是的,对于每个消息创建一个pthread,位被正确设置,然后线程终止。我没有一个线索为什么任何人会做这样的事情,但它提出了一个问题 - 实际创建线程有多少开销?

解决方案



...在串行端口上发送消息。对于每个消息创建一个pthread,正确设置位,然后线程终止。 ...实际创建线程有多少开销?



这是高度系统特定的。例如,上次我使用VMS线程是噩梦般的慢(多年,但从内存一个线程可以创建类似每秒10多次(如果你保持几秒钟没有线程退出你会核心)),而在Linux上,你可能创建了数千个。如果你想知道,在你的系统基准。但是,它不是很多使用只是知道没有知道更多的消息:他们平均5字节或100k,无论他们是连续发送还是线之间空闲,以及什么是应用程序的延迟要求都是相关的到代码的线程使用作为任何绝对测量的线程创建开销的适当性。而且性能可能不一定是主要的设计考虑。


I just reviewed some really terrible code - code that sends messages on a serial port by creating a new thread to package and assemble the message in a new thread for every single message sent. Yes, for every message a pthread is created, bits are properly set up, then the thread terminates. I haven't a clue why anyone would do such a thing, but it raises the question - how much overhead is there when actually creating a thread?

解决方案

...sends Messages on a serial port ... for every message a pthread is created, bits are properly set up, then the thread terminates. ...how much overhead is there when actually creating a thread?

This is highly system specific. For example, last time I used VMS threading was nightmarishly slow (been years, but from memory one thread could create something like 10 more per second (and if you kept that up for a few seconds without threads exiting you'd core)), whereas on Linux you can probably create thousands. If you want to know exactly, benchmark it on your system. But, it's not much use just knowing that without knowing more about the messages: whether they average 5 bytes or 100k, whether they're sent contiguously or the line idles in between, and what the latency requirements for the app are are all as relevant to the appropriateness of the code's thread use as any absolute measurement of thread creation overhead. And performance may not have needed to be the dominant design consideration.

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

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