[gameprogrammer] Re: variables and multithreading
- From: Kevin Jenkins <gameprogrammer@xxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Thu, 19 Jan 2006 20:35:43 -0800
Simply setting a variable won't work. For example:
Thread 1:
if (variable==false)
{
// Do my updates
}
Thread 2:
// Need to update my records
variable==true
// Do my stuff
variable==false
What can happen is that variable is set to true while thread 1 is inside
the block (where I have // Do my updates )
Checking for the start of a critical section lock doesn't work - however
checking at the end does if you only have two threads.
For example
bool done=false;
void myfunc(void)
{
// Do something
done=true;
}
if (done==true)
{
// I know whatever was used in // Do something is done and is not
currently in use.
done=false;
}
Therefore, it is safe to access a critical section in myfunc, before
done==true, and it is safe to access it in the if case that I posted
after that.
This is the same principle that is used in the single producer /
consumer queue. I use this in my network library RakNet
(http://www.rakkarsoft.com) in the file SingleProducerConsumer.cpp .
You have an array with a head and a tail. The head cannot read past the
tail, and the tail cannot write past the head. You don't increment
either until you are done with writing to a block. This is threadsafe
as long as one thread is the producer and the other is the consumer (and
there are only two threads)
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?
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- References:
- [gameprogrammer] variables and multithreading
- From: Roger D Vargas
Other related posts:
- » [gameprogrammer] variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
- » [gameprogrammer] Re: variables and multithreading
Roger D Vargas wrote:
- [gameprogrammer] variables and multithreading
- From: Roger D Vargas