In this lecture, I want to talk about poisoning.
We learned a lot in the previous lecture about new taxes and the benefits that they give us.
But you might have been wondering what would happen if the thread panicked while holding the lock.
When that occurs, our mutex has became poisoned.
This is obviously very undesirable because when this happens, all other threads are unable to access
the data since it is likely tainted.
So I went ahead and created an example here and I instantiated a new atomic reference counted mutex
and then I also cloned it into the lock two variable.
And now remember, our clone produces a new arc instance which points to the same allocation on the
heap as the source arc.
But it it increments its reference count.
So this arc is pointing to this one up here.
So inside here, we're going to take.
BLOCK and then we're going to panic.
So up here, when we acquire the lock, our lock is currently completely acceptable.
But calling the panic macro here, while this thread was holding the lock, our mutex is now poisoned
and for us to recover from this, we can do a match statement and we can try to match on what gets returned
when we try to access the lock that became poisoned and.
If Oak is return, then that's good.
We get our value back.
But if it's poisoned then we can call into inner to recover the mutex in the data inside of it.
And when we're able to do that, then we're able to access the data as normal and we will have our mutex
back.
And to demonstrate this, I can run this program and we see that I have printed out the value one.
Well, when I created the new mutex, our value was zero.
We poisoned it here.
And then in our match statement, we called into enter on it in order to recover it and then ID referenced
the value inside of it.
So here is basically zero plus zero.
Uh, zero plus one, and then we return one back and that is what was outputted back.
So it's important to have this match statement running in the event that you think one of your threads
might panic while holding the lock.
So it's just something for you to think about.