Is it safe to have multiple threads add to or subtract from a quantity?

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Jul 2008
Posts: 49
Reputation: OffbeatPatriot is an unknown quantity at this point 
Solved Threads: 0
OffbeatPatriot OffbeatPatriot is offline Offline
Light Poster

Is it safe to have multiple threads add to or subtract from a quantity?

 
-1
  #1
Jun 28th, 2009
Say I have a single floating point number called result equal to 0 and two threads, one thread adds 4 and the other subtracts 2. Is there any possibility that result will be equal something other then 2 afterwards?

If you want a more concrete example I ask this question because of a project I'm working on. Basically I'm simulating a network of neurons and I want to distribute the neurons among multiple threads so it runs faster.

Every thread is responsible for it's own group of neurons and at each time step of the simulation all threads calculate the voltage of their neurons and see if any neurons have exceeded a threshold, then they run into a barrier and wait for all the threads to catch up.

Then the voltages of any neurons connect to the neurons that exceeded the threshold are then added to or subtracted from depending on the strength of the connection. It's this step I worry about because two threads could try to change the voltage of the same neuron at the same time. I think I know a way around this but I wondered if things are fine the way they are, since the threads are adding or subtracting values as opposed to assigning values.

After this the threads run into the same barrier as before and wait for each other before starting the next time step.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #2
Jun 28th, 2009
Well you're expecting thread 1 to do this
load var
add 4
store var
and thread 2 to do this (before or afterwards),
load var
sub 4
store var
Then all will be well.


But if it actually goes like this (or many other variations which interleave the instructions),
load var
load var
add 4
store var
sub 4
store var
then you're going to have lots of very interesting Heisenbugs to deal with.

> Basically I'm simulating a network of neurons and I want to distribute the neurons among multiple threads so it runs faster.
Do you actually have a multi-core CPU?
If you don't, your statement is bogus.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #3
Jun 28th, 2009
You will require a semaphore to keep both threads from accessing the same data simultaneously.
Thread#1
{BLOCK}
Var += 4;
{UNBLOCK}

Thread#2
{BLOCK}
Var -= 4;
{UNBLOCK}

Otherwise you run the risk of both simultaneously accessing the same argument.

There is only one processor that I'm aware of that will allow you to change a memory location without needing a semaphore.

add mem[reg],4

Changes memory without being pre-empted.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 109
Reputation: Stinomus is on a distinguished road 
Solved Threads: 16
Stinomus Stinomus is offline Offline
Junior Poster

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #4
Jul 10th, 2009
Originally Posted by wildgoose View Post
You will require a semaphore to keep both threads from accessing the same data simultaneously.
A semaphore with only one token, more commonly known as a 'Lock'.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: jyh5 is an unknown quantity at this point 
Solved Threads: 0
jyh5 jyh5 is offline Offline
Newbie Poster

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #5
Sep 18th, 2009
I know you need a semaphore if you have two or more threads writing to a variable, but do you need them if you have one thread writing and all other threads reading?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #6
Sep 18th, 2009
With only one writing thread, and the others merely read, Depends on what the functionality does. Is it safe to use obsolete data!

Is only a single write operation required? Such as only writing 32-bits on a 32-bit processor?

If in either case is no, then if a standard mutex/semaphore/critical section is too slow, then a special lock is required!

If more then a single write is required then the thread can be pre-empted between a data write sequence. Also a thread can read a 32-bit value, get pre-empted by a 32-bit write and when gets released, will be working with old data! Having a locking mechanism is the safest!

It's kind of complicated, but the safest for now, is ful lock for read and write.
Last edited by wildgoose; Sep 18th, 2009 at 6:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: jyh5 is an unknown quantity at this point 
Solved Threads: 0
jyh5 jyh5 is offline Offline
Newbie Poster

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #7
Sep 26th, 2009
Originally Posted by jyh5 View Post
I know you need a semaphore if you have two or more threads writing to a variable, but do you need them if you have one thread writing and all other threads reading?
According to MS:
The Read method is unnecessary on 64-bit systems, because 64-bit read operations are already atomic. On 32-bit systems, 64-bit read operations are not atomic unless performed using Read.

Reference: http://msdn.microsoft.com/en-us/libr...cked.read.aspx

Therefore, 32 bit reads on 32 bit systems are atomic and don't need locking. However, adding and subtracting still needs locking. One simple way to do that in .net is using the "interlocked" class.
----
Writing is a different story and needs to be locked. An easy way to do that for simple types in .net is using interlocked.exchange

Reference: http://msdn.microsoft.com/en-us/libr....exchange.aspx
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #8
Sep 27th, 2009
On x86 systems you can do a locked add in one instruction.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Is it safe to have multiple threads add to or subtract from a quantity?

 
0
  #9
Oct 5th, 2009
Your link to Heisenbugs doesn't exist anymore or was linked incorrectly. http://en.wikipedia.org/wiki/Unusual_software_bug
Last edited by BestJewSinceJC; Oct 5th, 2009 at 2:50 am.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC