I have multiple threads in my program, one of which is operating on an internal data structure. Due to some error, this thread quits leaving the data structure in an invalid state. How can other threads properly validate the state of data structure on later access? In general, how to handle such a scenario?
Asked
Active
Viewed 33 times
0
-
1Exit the program and restart? Otherwise recovery depends on exactly what the errant thread did before failing, and if you know exactly what bugs you have you would presumably just fix them. – Alan Stokes Dec 23 '15 at 11:18
-
1It depends what you mean by "some error". Do you mean a code bug? A hardware problem? What kind of data structure? And what do you mean by "an invalid state"? This is way too vague to answer as asked. (Also, by tagging this with two languages, you've made it doubly broad as threading in those two languages is very different.) Why not ask about the specific case you're interested in, giving as much detail as possible? – David Schwartz Dec 23 '15 at 11:19
-
This is way too broad, but you may want to use [Code Contracts](http://stackoverflow.com/q/260817/96780). Also, why the two language tags? – Daniel Daranas Dec 23 '15 at 11:21
1 Answers
0
The best answer is to make sure that threads don't quit leaving the data structure invalid. Other than that, the only solution is something like:
In class:
bool m_data_valid = true; // Or possibly 'false' and set it true in constructor
In mutating thread:
m_data_valid = false;
... // Mutate structure
m_data_valid = true;
In other threads:
if (!m_data_valid)
fixup(); // Or whatever you were going to do.

Martin Bonner supports Monica
- 28,528
- 3
- 51
- 88