Hello there,
This is my first post in this forum and I have been trying to understand on my own what the presenter in that cpp conference is trying to say when he says that it is dangerous to load an atomic value in a destructor. He adds to say that the way to solve the issue is to have a load_nonatomic method.
1 2
|
class C { std::atomic<size_t> N, T* p, ... };
C::~C() { cleanup(p, N.load(std::memory_order_relaxed)) };
|
Basically, he implies that N could be accessed by another thread while being destroyed.
But in the example above, the cleanup argument will be evaluated before going through the function body's instruction set.
What is the big deal about using an atomic operation inside the destructor? Is it due to the fact that a read can start (and not complete) at the time the destructor has already destroyed the atomic value resulting in an undefined behavior? If so, that could happen even in the case you use a load_nonatomic read ?
Basically, I don't see where the issue is exactly and unfortunately, the presenter made a joke about it being dangerous but omitted completely explaining what's going on behind the curtains.
Link to the talk. The exact timestamp is 56:37.
https://www.youtube.com/watch?v=ZQFzMfHIxng&t=3396s
Thank you very much in advance,
P.S.: I've watched the presentation three times and also trying to search online using keywords such as 'atomic read load and destructor' to no avail. This is bothering me as I would like to understand how things work rather than blindly follow guidelines, which is what was done in that presentation.