C线程值得重用吗?
是否值得花时间编写代码以在 C 中重用线程,或者它们的创建和销毁成本低廉吗?
我正在渲染一些仅 CPU 的 3D 图形,速度非常慢(看起来大约 5 fps)。我尝试使用线程来解决这个问题。使用 4 个线程,每个线程渲染屏幕的一条带,似乎将我的帧速率提高到非常合理和流畅的水平。我仍然担心当我让我的图形变得更复杂时会发生什么。通过重用我的线程而不是每帧创建和销毁它们,我会获得明显的速度提升吗?
编辑:我正在使用的操作系统是Windows。
Is it worth taking the time to write the code to reuse threads in C, or are they cheap to create and destroy?
I'm rendering some CPU only 3D graphics and it was going pretty slow (looked like about 5 fps). I tried using threads to solve this. Using 4 threads, each rendering a strip of the screen, seemed to boost my frame rate to something very reasonable and smooth. I'm still worried about what will happen when I make my graphics more complicated. Would I get any appreciable speed boost by reusing my threads instead of creating and destroying them every frame?
Edit: The operating system I'm working on is Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于使用 1 个线程时,您可以获得大约 5 fps,大概使用 4 个线程时,您会达到大约 20 fps 吗?那么,如果不保留它们,您每秒将创建和销毁 80 多个线程?线程相当轻量,但我想您会开始注意到这么多的开销。
Since with 1 thread you got about 5 fps, presumably with 4 threads, you got to somewhere around 20 fps? So, you would be creating and destroying 80+ threads per second if you don't keep them around? Threads are fairly lightweight, but I think you'd begin to notice that much overhead.
是的,您将消除大量开销和延迟。说实话,我不明白为什么这么多开发人员一开始就不断地创建和销毁线程——它效率低下,困难重重,而且容易出现泄漏和其他灾难。
You will eliminate quite a lot of overhead and latency, yes. TBH, I can't understand why so many developers start off by continually creating and destroying threads - it's inefficient, difficult and prone to leaks and other disasters.