SDL_LockSurface has nothing to do with concurrency. When you “lock” a
surface, you are really just obtaining a pointer to the surface
contents in system memory, if the surface was in graphics memory.
Hence the macro SDL_MUSTLOCK.
Lets say you replaced the locking in the snippet you gave with actual
concurrency primitives. In this case it is debatable whether you would
gain much from multi-threading, as you could be suffering from locking
contention. Excessive locking can make the code run in a single
threaded fashion - or slower (due to context switching overhead). It
can also produce deadlocks etc.
The proper way to do multi-threading is to see:
- which algorithms can be parallelised by itself
- which algorithms can be done concurrently without breaking things
Graphics usually can be - this is why graphics cards tend to have
hundreds of cores. Doing it correctly is challenging. It depends on
what you are doing.
Firstly, is performance a problem? If so, are you sure that the
blitting is the bottleneck?
There are options other than having many blit threads. Perhaps you
could do all the blitting in one thread and concurrently run the game
logic in another thread, etc. I believe that some windowing systems,
like Windows, forces all screen blits to occur in the main thread
anyway. I think you should be safe using SDL’s software blits
outside the main thread. In general though it might be easier to keep
all graphical stuff in a single thread.
The alternative for heavy rendering would be to use OpenGL, thus
taking full advantage of any hardware in an asynchronous fashion.
Its hard to give concrete advice on a subject as complex as
multi-threading without additional information. There is no one true
way, each case needs to be examined individually to see how it can
make the most of multiple threads.On Tue, Jun 24, 2008 at 10:04 AM, julien CLEMENT wrote:
In a multi-threaded application with concurrent access to SDL_Surface’s
(including the display),
which may include blit, update, or even direct access to pixels, what would
you suggest to
prevent inconsistent blits ?