SDL:在多线程程序中这样做安全吗?
我有一个线程执行以下操作:
- 初始化 SDL
- 存储指向 SDL_Surface 的指针
- 进入循环并等待任何鼠标事件并处理它们
在另一个线程中,有一个函数执行以下操作:
- 获取指向 SDL_Surface 的指针
- 执行SDL_LockSurface
- 操作像素
- SDL_UnlockSurface 是否
- 在表面上调用 SDL_Flip
我在文档中读到,通常 SDL lib 函数调用都应该来自同一线程。这是否包括直接更改 SDL_Surface?使用表面的锁定和解锁功能怎么样?我认为这些锁定和解锁对旨在用于多线程情况。
SDL_Flip 函数怎么样?如果需要从初始化 SDL 的 SDL 线程调用它,那么我可以简单地发出用户事件信号并在另一个线程中处理它。
I have a thread that does the following:
- Initializes SDL
- Stores a pointer to the SDL_Surface
- Goes into a loop and waits for any mouse events and processes them
In another thread there is a function that does the following:
- Gets the pointer to the SDL_Surface
- Does a SDL_LockSurface
- Manipulates the pixels
- Does a SDL_UnlockSurface
- Calls SDL_Flip on the surface
I have read in the documentation that generally SDL lib function calls should all be from the same thread. Does this include directly changing an SDL_Surface? How about using the lock and unlock functions for the surface? I would think these lock and unlock pair are intended to be used in multi-threaded situations.
How about the SDL_Flip function? If this needs to be called from the SDL thread that initialzed SDL, then I could simply signal a user event and handle it in the other thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SDL_Surfaces 上的锁定/解锁是为了处理将位图放置在系统内存以外的位置的后端。锁定表面会将位图拉回到系统内存中进行修改,而解锁则将其推出。
它们不适用于多线程。
您可能可以通过在主线程中锁定/解锁表面并将位图指针传递给工作线程来完成。
The lock/unlock on SDL_Surfaces are to handle backends that place bitmaps in something other than system memory. Locking a surface pulls the bitmap back into system memory for modifications, while unlock pushes it back out.
They are not for multithreading.
You might be able to get by with locking/unlocking the surface in the main thread and passing the bitmap pointer to your worker thread.