Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 653
waffleman Avatar asked Oct 28 '25 18:10

waffleman


1 Answers

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.

like image 176
genpfault Avatar answered Oct 30 '25 08:10

genpfault