what is "Thread Safety"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Whats all this business about thread safety?
Where can i learn or get details of this.

From my understanding it has to do with issues arising from multiple threads
accessing/trying to access the same resource(s).

Please could someone give me a link or shed more light on this, as i'm not
sure i understand this well enough (as i see this all over the place)
 
Hello Luke,

See there http://en.wikipedia.org/wiki/Thread-safety

L> Whats all this business about thread safety?
L> Where can i learn or get details of this.
L> From my understanding it has to do with issues arising from multiple
L> threads accessing/trying to access the same resource(s).
L>
L> Please could someone give me a link or shed more light on this, as
L> i'm not sure i understand this well enough (as i see this all over
L> the place)
L>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
On top of that excellent WikiPedia link I would like to add a more concrete
thread-unsafe implementation exemple:

class ThreadDisasterCounter
{
int counter;

public int Value { get { return counter; } }

public void Inc()
{
counter = couter + 1;
}
}

now imagine 2 thread are calling inc at the same time!
in assembly
counter = couter + 1; is rougly equivalent in pseudo assembly to

load memory[counter] to register dx
inc dx
write dx to memory[counter]

now you could imagine with 2 unlucky thread:
thread 1 load memore[counter] to register dx
=> premption
thread 2 load memory[counter] to register dx
thread 2 inc dx
thread 2 write dx to memory [counter]
=> preemption (register restored to previous value)
thread 2 inc dx (which is its original value)
thread 2 write dx to memory [counter]

now 2 thread called Inc() but the value has increased only by one, see?
of course this exemple is quite simple, you could mess uup thing a lot more.

how do you avoid thread unsafety?
- all your thread access only read-only / unmutable common variable
- all mutable data are on private stack

and lock: any write AND read access to variable that could change is in a
lock
lock(anObject)
{
// critical section
}


read should also lock as show in this simple exemple
class DisatrousThreadedSwap
{
public object Object1;
public object Object2;

public void Swap()
{
object tmp = Object1;
Object1 = Object2; <= a preemption here will cause disatrous read
Object2 = tmp;
}
}

class ThreadSafeSwap
{
object obj1, obj2;
public object Object1
{
get { lock(this) return obj1; }
}

public object Object1
{
get { lock(this) return obj2; }
}
public void Swap()
{
lock(this)
{
object tmp = obj1;
obj1 = obj2;
obj2 = tmp;
}
}
}

To finish these exemples thread safaty is said to be expensive so don't
write general purpose thread safe class.
Instead ensure the specific part of your app that use thread make it safe by
either use stack local instance or appropriate usage of readonly class or
lock....
 
Back
Top