Concurrent programming bug - the solution
In my last post I outlined a bug that recently bit me in a reference-counting mechanism in a concurrent system.
If you haven't solved the mystery yet, here's some hints from common guesses I've seen from various people:
- The reference count is implemented using atomic intrinsics.
- Atomicity and alignment are proven correct for the platform in question.
- Mutual exclusion and other locking mechanisms are not necessary for the solution.
- Reference counts are "correct" at all times in that there is no leaked reference.
- RAII is already in use, so it will not magically make the problem go away.
Spoiler hint 1
Spoiler hint 2
A couple lucky people picked up the solution pretty fast, but for the most part this seems like something that most programmers I've shown this to are not thinking about. Ironically, a few have had "aha!" moments where they recalled various coding conventions and rules about reference counting, and suddenly understood why those rules exist.
Thanks for playing!