Is it safe to have multiple threads add to or subtract from a quantity?
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.
OffbeatPatriot
Junior Poster in Training
68 posts since Jul 2008
Reputation Points: 15
Solved Threads: 0
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
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.
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
On x86 systems you can do a locked add in one instruction.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354