[gameprogrammer] Re: variables and multithreading

Roger D Vargas wrote:

Im writing a multithreaded server and I have reached the point where I have to access the array containing the players data from the main loop. Should I use some variable to mark if a record is being modified by network thread?

Well, you should use more than a variable, you should use a mutex or a semaphore (these are special variable s that ensures mutual exclusion between different threads). If you don't, things will go bad sooner or later. Even if the network thread is the only one writing to the player data, it might be preempted in the middle of an update, and the main thread will read inconsistent data.

I suggest you read a tutorial on mutexes and semaphores (the theory behind all mutexes/semaphores implementation is the same, only the function names change). Or you could look at SDL which implements this stuff, the functions are quite simple and highly portable :
http://www.libsdl.org/cgi/docwiki.cgi/SDL_20API


Stephane




--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: