A
Angus
Hi
I have written a program which runs two threads. One thread controls
the creation and modification of an object used in the program.
Another thread periodically checks a list of these objects and deletes
an object from the list if no longer required.
I have written a class to wrap the operating system specific lock /
unlock of the object. This works fine. But I get the following
problem:
1. thread 1 creates object1.
2. thread 2 checks object1 to see if ready for deletion and it
eroneously considers it is.
3. thread 1 modifies object1.
4. (simultaneously with 3.) deletes object1.
Result is an access violation as at 3. a pointer to object1 is
dereferenced.
My possible solutions for this problem are:
1. Add a lock and unlock function to the object1 class. Then have to
lock, perform manipulation, then unlock. But then if a instruction
was waiting for the access control/critical section to free up and the
object gets deleted - then what happens?
2. Don't actually delete object1. Simply mark it as deletable. then
have another thread check when possible to delete object1 say every
hour - when there is no chance that thread1 would be doing anything
with object1.
Noe of these solutions seems ideal. Or maybe I am not understanding
correctly.
Does anyone have any suggestions?
Angus
I have written a program which runs two threads. One thread controls
the creation and modification of an object used in the program.
Another thread periodically checks a list of these objects and deletes
an object from the list if no longer required.
I have written a class to wrap the operating system specific lock /
unlock of the object. This works fine. But I get the following
problem:
1. thread 1 creates object1.
2. thread 2 checks object1 to see if ready for deletion and it
eroneously considers it is.
3. thread 1 modifies object1.
4. (simultaneously with 3.) deletes object1.
Result is an access violation as at 3. a pointer to object1 is
dereferenced.
My possible solutions for this problem are:
1. Add a lock and unlock function to the object1 class. Then have to
lock, perform manipulation, then unlock. But then if a instruction
was waiting for the access control/critical section to free up and the
object gets deleted - then what happens?
2. Don't actually delete object1. Simply mark it as deletable. then
have another thread check when possible to delete object1 say every
hour - when there is no chance that thread1 would be doing anything
with object1.
Noe of these solutions seems ideal. Or maybe I am not understanding
correctly.
Does anyone have any suggestions?
Angus