— Bob Pendleton wrote:
Hi,
I’m using a thread in my program for network stuff
while the main loop
of my game continues. Everything works fine, and I
can get a message
from the server and printf it, but if I try to do
anything else, like
just incrementing a variable for the amount of
players I get a seg
fault (Fatal signal: Segmentation Fault (SDL
Parachute Deployed)).
This sounds a lot like memory corruption. Is your
network code writing
an arbitrary amount of information to a fixed size
buffer?
This is, of course possible, but I suspect something
else.
I tried making the net thread call another
function to do what was
making it seg fault but that has not fixed it.
Is there any “scope” of these threads I should
know about?
Only that you have to make sure that two threads
don’t use the same
variables at the same time. For example, if two
threads try to assign
the same variable at the same time the results are
anyones guess.
True, but the mechinism usually used for doing this is
called a “Mutex” (shot for mutually exclusive
variable). You don’t store any DATA in a mutex, you
simply use it to broker authority to write to some
OTHER variable or data structure. So, if you’re going
to increment your # of players from your network
thread, the
network thread would:
“lock” the mutex prior to incrementing
do the increment, then
"unlock" the mutex
Then before reading, or modifying the value, the main
thread would:
“lock” the mutex prior to reading
use the variable,
but not “unlock” the mutex until the value is safe
to modify again.
If one thread is trying to lock a mutex that’s already
locked, it will “block” the thread until it’s safe for
it to aquire the lock.
Try hard to not assume that operations are atomic.
Your simple variable++ example involves at least:
read variable
increment variable
write variable
What happened if the main thread tried to read the
variable part-way through the write operation? Also I
suggest you consider making any variables accessed my
multiple threads “volitile”. That should ensure they
are flushed out to memory after every operation.
I’m really baffled as to why I can do things like
printf but not a
simple variable++!!!
Yeah that can be pretty painful. Trying stubbing out
routines until the
program works and then put the code back in until it
fails. That will
tell you where the problem is.
While this is a quite reasonable suggestion with
single-threaded code, it is quite foolhearty for
multi-threaded code. Multi-threaded code is very
sensitive to timing issues, and, if not written
properly, will often work fine one machine, but not on
a seemingly identical CPU. CPU lot number, or even
differences in cooling can add enough variation to
make poorly written multi-threaded code misbehave.
Be sure to be vigilant about using mutexes, even where
it seems a bit trivial. (Never assume an operation is
atomic… ESPECIALLY any sort of WRITE operation.)
Hope this helps,
-Loren> On Wed, 2003-10-01 at 02:48, Timothy Hanlon wrote:
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com