Odd threading issues

I’m creating a networked 4-player puzzle game, and I’m doing my simulation
of each of the four playing fields in different threads. The simulation
itself runs fine on all the threads, but when the threads return,
sometimes “Dr Watson,” which I’m guessing to be the Win32 thread manager
of some kind, errors and complains that it could not attach to a thread,
with a message saying “Windows 2000 error code = 87 / The parameter is
incorrect.” Then the program itself crashes with error message " has generated errors and will be closed by windows." It’s
not consistent, and does not occur at all when I only run the Renderer and
one player simulation. It occurs with greater frequency the more
simulation threads I have. I’m really not sure what’s going on, I’m kind
of new to this. Here’s my code calling the threads, and my event poller
(quit is a global bool, and is the test condition for the while loop that
keeps the threads from returning). Thanks for any help.

~Greg Peele

printf("Starting rendering\n");
Renderer = SDL_CreateThread(RenderScreen, NULL);

player.simThread = SDL_CreateThread(Testing_Pattern, &player);
printf("Player simulation thread started\n");
opponent1.simThread = SDL_CreateThread(Testing_Pattern, &opponent1);
printf("Opponent 1 simulation thread started\n");
opponent2.simThread = SDL_CreateThread(Testing_Pattern, &opponent2);
printf("Opponent 2 simulation thread started\n");
opponent3.simThread = SDL_CreateThread(Testing_Pattern, &opponent3);
printf("Opponent 1 simulation thread started\n");

while(!quit)
  {
    SDL_Event event;
    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT || (event.type == SDL_KEYDOWN &&

event.key.keysym.sym == SDLK_ESCAPE))
{
quit = true;
SDL_WaitThread(player.simThread, NULL);
SDL_WaitThread(opponent1.simThread, NULL);
SDL_WaitThread(opponent2.simThread, NULL);
SDL_WaitThread(opponent3.simThread, NULL);
SDL_WaitThread(Renderer, NULL);
}
}
}
return 0;

Don’t render from a separate thread. Do the logic processing in the other
threads and do the rendering from the main thread. Make sure you’re locking
all the shared data appropriately.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Erf, sorry to the moderator for posting from an unregistered email address
by accident. Sorry if this post shows up twice.

Anyways, I’m developing a networked multiplayer puzzle game, but I’m
wondering if I’m handling my threads right. There’s a total of 6 threads,
4 of which are the same function called on different objects, and they do
what they’re supposed to do as far as data manipulation and display (they
never, ever write to overlapping parts of any surfaces). Except on exit.
They either crash a slow generic windows crash, or a fast Dr. Watson crash
(I’m running Windows 2000). The threads all test for the truth of a
global bool as the condition for the while loop that keeps the thread from
returning. Based on various experimentation via cerr, the threads all
reach their returns statements just fine, but the program crashes after it
hits one of the SDL_WaitThread() calls. Which particular thread it
crashes on seems to be random, and very, very rarely, there is no crash.
The threads are all well-behaved and don’t run into any concurrency
problems (so far as I know), and in fact, the crash still occurs if I
comment out the body of the thread functions inside the while loop I
mentioned and replace it with SDL_Delay(0). The crashing seems to be less
and less frequent the fewer threads I create (I comment out the
SDL_CreateThread and the appropriate SDL_WaitThread to test this). It
stops crashing entirely when there is only one thread being created. Any
ideas what I"m messing up?

Thanks (sorry, I keep on posting on this topic :frowning: )
Greg Peele

Thread call:
int main() {
// previous part of main, probably irrelevant

player.simThread = SDL_CreateThread(SimulateField, &player);
opponent1.simThread = SDL_CreateThread(SimulateField, &opponent1);
// etc

// event processing loop goes here, sets quit to true on SDLK_ESCAPE or
// SDL_QUIT. exits when quit is true.

SDL_WaitThread(player.simThread, NULL);
cerr << “Player thread has exited.\n”; // usually prints out.
SDL_WaitThread(opponent1.simThread, NULL);
cerr << “Opponent 1 thread has exited.\n”; // might not print out
// etc

return 0;
}

int SimulateField(void *data)
{
// minor initialization code, casting the pointer to its proper type
// works fine

while (!quit)
{
// do some things.
// crash occurs even when this body is blank, though.
}
cerr << “Player " << playerNum << " simulation thread exiting\n”;
// always prints out for all threads.
return 0;
}