views:

48

answers:

1

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.

+2  A: 

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.

genpfault
SDL_LockSurface() and SDL_UnlockSurface are nops on most platforms. I can't remember exactly right now, but I think they are only needed on either GDI or DirectX. In any case, they are absolutely useless for synchronizing threads.
ninjalj