问题描述:

I have been working with a simple bi-threaded application with two threads running simultaneously. Each thread plots a figure(a triangle/ a rectangle) to a predefined and mutually exclusive section of the screen. That is, both threads, never write in each other's screen space.

After running the program for a long time, say 5hours or so, the main thread keeps running , but the other thread freezes(it doesn't die though).

I get this in backtrace o/p, which shows a deadlock I guess.

Thread 2 (Thread 0xb542f440 (LWP 2142)):

#0 0xb6e83258 in __lll_lock_wait ()

from /lib/arm-linux-gnueabihf/libpthread.so.0

#1 0xb6e7de38 in pthread_mutex_lock ()

from /lib/arm-linux-gnueabihf/libpthread.so.0

#2 0xb6ef8de4 in SDL_mutexP ()

from /usr/lib/arm-linux-gnueabihf/libSDL-1.2.so.0

#3 0xb6ef4058 in ?? () from /usr/lib/arm-linux-gnueabihf/libSDL-1.2.so.0

#4 0xb6ef4058 in ?? () from /usr/lib/arm-linux-gnueabihf/libSDL-1.2.so.0

Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Is it really a libSDL1.2 issue? Do I have to write explicit user defined mutex for every SDL_Blit/or other operations with SDL, that I am doing? Or is it something else that I am missing? How would I solve this issue?

I am using:

libSDL1.2 available in Raspberry Pi

libPthreads

libSDL_ttf

EDIT: gdb trace of the thread 2 mutex:

(gdb) p *(pthread_mutex_t*) 0xb542ed38

$3 = {_data = {_lock = 39257160, __count = 1048617, __owner = 0,

__kind = 7078021, _nusers = 0, {_spins = 0, _list = {_next = 0x0}}},

__size = "H\004W\002)\000\020\000\000\000\000\000\205\000l\000\000\000\000\000\000\000\000", __align = 39257160}

Let me know if you need more information.

网友答案:

This approach will only work with software surfaces (w/o hardware acceleration). With hardware surfaces it, at best, would lock and wait - which have no performance benefits anyway; and in bad case (judging by quick look at SDL source) it could end badly, by the simple logic how SDL_LockSurface works.

I'm not even sure your external mutexes would help, since hardware accelerated graphics access should almost always be locked to one thread (OpenGL is good example - each thread have it's own separate drawing context).

In short, i see no reason why you should use two threads for drawing, but in any case it isn't guaranteed to be thread-safe.

相关阅读:
Top