C线程值得重用吗?

发布于 2025-01-03 02:37:01 字数 242 浏览 0 评论 0原文

是否值得花时间编写代码以在 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 技术交流群。

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

发布评论

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

评论(2

攀登最高峰 2025-01-10 02:37:01

由于使用 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.

时光沙漏 2025-01-10 02:37:01

是的,您将消除大量开销和延迟。说实话,我不明白为什么这么多开发人员一开始就不断地创建和销毁线程——它效率低下,困难重重,而且容易出现泄漏和其他灾难。

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.

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