[gameprogrammer] Re: variables and multithreading

I sent this a couple of days ago be realized it never
went through for some reason, so I'm doing it again:

--------------------------------------------------

You need to use a mutex.  A mutex is a shared resource
for threads, but only one thread may own the lock at a
time, and if another thread tries to lock it while
it's already locked, it'll hang until the first thread
releases the lock.

 

Therefore, in all of you threads that need to access
the shared array you'd need something like this,
assuming you?re using Posix threading:

 

Define a GLOBAL mutex variable:

 

pthread_mutex_t shared_list_lock;

 

Do this just once (not in each thread)

 

pthread_mutex_init( &shared_list_lock );

 

Do this in each thread

 

pthread_mutex_lock( &shared_list_lock );

...

Do work here

...

pthread_mutex_unlock( &shared_list_lock );

 

 

Hope this helps

 

Larry

 

 

> -----Original Message-----

> From: gameprogrammer-bounce@xxxxxxxxxxxxx
[mailto:gameprogrammer-

> bounce@xxxxxxxxxxxxx] On Behalf Of Kevin Jenkins

> Sent: Thursday, January 19, 2006 11:36 PM

> To: gameprogrammer@xxxxxxxxxxxxx

> Subject: [gameprogrammer] Re: variables and
multithreading

> 

> 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

 



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


Other related posts: