billw
February 17th, 2007, 07:29 PM
I am trying to run a number of threads simultaneously, and running into what appears to be deadlock problems (if I understand the term correctly):
thread A locks resource A
thread B locks resource B
thread A trys to lock resource B but cannot so waits here
thread B trys to lock resource A but cannot so waits here
What design considerations should I be making to avoid these problems?
The data used in the threads is a shared scenegraph in a 3D application I am writing, how should I go about making these classes thread safe?
It seems to me that as soon as a call is made to a function within a locked area of code there is the possiblity of deadlocks (as you don't know what is being locked within the functions code).
e.g.
public void someFunc()
{
lock(this)
{
someOtherFunc(); // <--- not thread safe?
}
}
Also as soon as you allow a reference to a field member of an class to pass outside the code of the object itself it becomes thread unsafe, as the client is then responsible for locking the data.
e.g.
private List<int> numbers;
public List<int> Numbers
{
get{ lock(this) return numbers; } // <--- thread unsafe even with a lock, because "numbers" is a reference?
}
That means either access to objects stored within other objects must be done through an intemediary threadsafe interface, or the objects themselves must be known to be threadsafe.
The ammount of different ways that an object, its code and its data can become thread unsafe is making my head spin!
Are there any foolproof guide lines I can follow to write thread safe code? Like a best practices but for threads.
Thanks!
thread A locks resource A
thread B locks resource B
thread A trys to lock resource B but cannot so waits here
thread B trys to lock resource A but cannot so waits here
What design considerations should I be making to avoid these problems?
The data used in the threads is a shared scenegraph in a 3D application I am writing, how should I go about making these classes thread safe?
It seems to me that as soon as a call is made to a function within a locked area of code there is the possiblity of deadlocks (as you don't know what is being locked within the functions code).
e.g.
public void someFunc()
{
lock(this)
{
someOtherFunc(); // <--- not thread safe?
}
}
Also as soon as you allow a reference to a field member of an class to pass outside the code of the object itself it becomes thread unsafe, as the client is then responsible for locking the data.
e.g.
private List<int> numbers;
public List<int> Numbers
{
get{ lock(this) return numbers; } // <--- thread unsafe even with a lock, because "numbers" is a reference?
}
That means either access to objects stored within other objects must be done through an intemediary threadsafe interface, or the objects themselves must be known to be threadsafe.
The ammount of different ways that an object, its code and its data can become thread unsafe is making my head spin!
Are there any foolproof guide lines I can follow to write thread safe code? Like a best practices but for threads.
Thanks!