如何正确使用SDl_Threads?

时间:2022-04-30 00:08:14

I am new to threads,SDL and how graphic work in general. I've been looking through all of LazyFoo's SDL tutorials, and had helped me greatly. But in his tutorials about multi threading, he commented that you should never use video functions in separate threads, or might cause problem. I am curious how it should be done, as I still have a vague understanding of graphics and threads.

我是线程,SDL和一般图形工作的新手。我一直在浏览所有LazyFoo的SDL教程,并且对我帮助很大。但是在他关于多线程的教程中,他评论说你不应该在单独的线程中使用视频函数,否则可能会导致问题。我很好奇它应该如何完成,因为我仍然对图形和线程有一个模糊的理解。

As one of my projects is a shoot'em up, I was wondering if I should create one thread that displays all the graphics, one threads receives all the player input for his ship, and another thread for the enemy AI.

由于我的一个项目是射击,我想知道我是否应该创建一个显示所有图形的线程,一个线程接收他的船的所有玩家输入,另一个线程用于敌人AI。

If this is NOT how it should be done, (I think it's wrong) does anyone have any advice of how graphics should be implemented with user input and enemy AI with threads?

如果这不是应该怎么做,(我认为这是错的)有没有人有任何关于如何用用户输入和带有线程的敌人AI实现图形的建议?

For the Lazyfoo's tutorials, this is the link: http://lazyfoo.net/SDL_tutorials/

对于Lazyfoo的教程,这是链接:http://lazyfoo.net/SDL_tutorials/

2 个解决方案

#1


Generally I think it is a good idea to separate the rendering thread from the UI/AI threads. It helps to keep your code more understandable. As there are many games available in source code, why don't you check out how they did it?

一般来说,我认为将渲染线程与UI / AI线程分开是个好主意。它有助于保持代码更易理解。由于源代码中有许多游戏,为什么不查看他们是如何做到的?

#2


It can be a lot easier to separate threads on a responsibilities basis when you're starting out. The more "correct" way of approaching this, which leads to greater scalability, is to use thread pools. There are pre-built thread pool solutions out there, including one for SDL.

在您开始时,基于职责分离线程可能要容易得多。接近这一点的更“正确”的方式是使用线程池,从而提高可扩展性。有预先构建的线程池解决方案,包括一个用于SDL的解决方案。

If you're using OpenGL or DirectX to do rendering, I doubt that the calls to those APIs will generate much overhead, so you could at least do the (necessarily) sequential parts of your game loop in the main thread: get input, update world model (including using results of ai calcs), render. Then you can have AI, network threads running in parallel. Thus you don't need to worry about a separate rendering thread. There is one exception to this: if you are doing computationally heavy (and I mean as in raycasting/raytracing heavy) rendering in software, then you would be better off somehow performing rendering "logic" on a parallel thread, writing to a framebuffer, and then in your main thread doing the sequence given above, and just blitting your prepared framebuffer using eg. GDI.

如果您使用OpenGL或DirectX进行渲染,我怀疑对这些API的调用会产生很多开销,因此您至少可以在主线程中执行游戏循环的(必要的)顺序部分:获取输入,更新世界模型(包括使用ai计算结果),渲染。然后你可以让AI,网络线程并行运行。因此,您无需担心单独的渲染线程。有一个例外:如果你在软件中进行计算量很大(我的意思是像在光线投射/光线跟踪中那么重)渲染,那么你最好以某种方式在并行线程上执行渲染“逻辑”,写入帧缓冲区,然后在你的主线程中执行上面给出的序列,并使用例如blitting你准备好的帧缓冲区。 GDI。

In regards to rendering, yes AFAIK rendering from only a single thread (the main thread) is (or was) generally a good idea to ensure that eg. with OpenGL you have no problems. Again AFAIK this is no longer an issue on the major platforms. Also, I don't know whether GDI or DirectX separately have the same problems.

关于渲染,是的,只有一个线程(主线程)的AFAIK渲染通常是一个好主意,以确保例如。使用OpenGL你没有问题。再次AFAIK这不再是主要平台上的问题。另外,我不知道GDI或DirectX是否也有同样的问题。

#1


Generally I think it is a good idea to separate the rendering thread from the UI/AI threads. It helps to keep your code more understandable. As there are many games available in source code, why don't you check out how they did it?

一般来说,我认为将渲染线程与UI / AI线程分开是个好主意。它有助于保持代码更易理解。由于源代码中有许多游戏,为什么不查看他们是如何做到的?

#2


It can be a lot easier to separate threads on a responsibilities basis when you're starting out. The more "correct" way of approaching this, which leads to greater scalability, is to use thread pools. There are pre-built thread pool solutions out there, including one for SDL.

在您开始时,基于职责分离线程可能要容易得多。接近这一点的更“正确”的方式是使用线程池,从而提高可扩展性。有预先构建的线程池解决方案,包括一个用于SDL的解决方案。

If you're using OpenGL or DirectX to do rendering, I doubt that the calls to those APIs will generate much overhead, so you could at least do the (necessarily) sequential parts of your game loop in the main thread: get input, update world model (including using results of ai calcs), render. Then you can have AI, network threads running in parallel. Thus you don't need to worry about a separate rendering thread. There is one exception to this: if you are doing computationally heavy (and I mean as in raycasting/raytracing heavy) rendering in software, then you would be better off somehow performing rendering "logic" on a parallel thread, writing to a framebuffer, and then in your main thread doing the sequence given above, and just blitting your prepared framebuffer using eg. GDI.

如果您使用OpenGL或DirectX进行渲染,我怀疑对这些API的调用会产生很多开销,因此您至少可以在主线程中执行游戏循环的(必要的)顺序部分:获取输入,更新世界模型(包括使用ai计算结果),渲染。然后你可以让AI,网络线程并行运行。因此,您无需担心单独的渲染线程。有一个例外:如果你在软件中进行计算量很大(我的意思是像在光线投射/光线跟踪中那么重)渲染,那么你最好以某种方式在并行线程上执行渲染“逻辑”,写入帧缓冲区,然后在你的主线程中执行上面给出的序列,并使用例如blitting你准备好的帧缓冲区。 GDI。

In regards to rendering, yes AFAIK rendering from only a single thread (the main thread) is (or was) generally a good idea to ensure that eg. with OpenGL you have no problems. Again AFAIK this is no longer an issue on the major platforms. Also, I don't know whether GDI or DirectX separately have the same problems.

关于渲染,是的,只有一个线程(主线程)的AFAIK渲染通常是一个好主意,以确保例如。使用OpenGL你没有问题。再次AFAIK这不再是主要平台上的问题。另外,我不知道GDI或DirectX是否也有同样的问题。