C++ already gives us a tool for pointer zapping
C++ already gives us a tool for pointer zapping
Posted Oct 16, 2024 13:55 UTC (Wed) by tialaramex (subscriber, #21167)In reply to: C++ already gives us a tool for pointer zapping by tialaramex
Parent article: Zapping pointers out of thin air
You do need to be careful. Hazard Pointers let you delay reclamation and avoid the aliasing problem here for example.
Thread 1 finds the front of the LIFO is A, it successfully points its HP at A, then it makes B, then it connects B to A, it's about to attempt the compare-exchange but then...
Thread 2 interrupts, it marks A for reclamation, but A cannot be reclaimed immediately because there's a Hazard Pointer pointing at it, so it's on the "reclaim later" pile. It makes C, and puts C at the front of the LIFO
Thread 1 resumes, it tries to atomically replace A with B at the front of the LIFO, this fails A.addr() != C.addr() and so thread 1 learns that C is now at the front. It releases the HP and points it successfully at C instead, it adjusts the address inside B to connect to C instead and this time a compare-exchange succeeds, swapping C for B.
Some time later, for any reason, reclamation is attempted for A and it now succeeds.
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
