开发者

SDL: Is this safe to do in a multi-threaded program?

开发者 https://www.devze.com 2023-01-19 03:38 出处:网络
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

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 sign开发者_运维知识库al a user event and handle it in the other thread.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消