SDL:在多线程程序中这样做是否安全?

时间:2022-03-13 21:03:24

I have a thread that does the following:

我有一个执行以下操作的线程:

  • Initializes SDL
  • Stores a pointer to the SDL_Surface
  • 存储指向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
  • 获取指向SDL_Surface的指针

  • Does a SDL_LockSurface
  • 是SDL_LockSurface

  • Manipulates the pixels
  • 操纵像素

  • Does a SDL_UnlockSurface
  • 是否SDL_UnlockSurface

  • Calls SDL_Flip on the surface
  • 在表面上调用SDL_Flip

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.

我在文档中已经读到SDL lib函数调用通常都来自同一个线程。这是否包括直接更改SDL_Surface?如何使用表面的锁定和解锁功能?我认为这些锁定和解锁对旨在用于多线程情况。

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.

SDL_Flip函数怎么样?如果需要从初始化SDL的SDL线程调用,那么我可以简单地发信号通知用户事件并在另一个线程中处理它。

1 个解决方案

#1


4  

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.

SDL_Surfaces上的锁定/解锁是为了处理将位图放在系统内存以外的位置的后端。锁定表面会将位图拉回系统内存以进行修改,而解锁会将其推回原位。

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.

您可以通过锁定/解锁主线程中的表面并将位图指针传递给工作线程来实现。

#1


4  

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.

SDL_Surfaces上的锁定/解锁是为了处理将位图放在系统内存以外的位置的后端。锁定表面会将位图拉回系统内存以进行修改,而解锁会将其推回原位。

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.

您可以通过锁定/解锁主线程中的表面并将位图指针传递给工作线程来实现。