So what the code does is use an AtomicPtr that points to the heap where the data is stored, and multiple threads mutate the data via the AtomicPtr.
Thought that the mutation of the data is within one atomic operation, so race shouldn't happen. What did I miss?
If I misused AtomicPtr, then what is the practical use? Any examples? Thanks.
Edit: To add on, I'm trying to build a tree-like data structure where the nodes are atomic. Knowing that Mutex and Convar would do the job, but I was being naive in trying something stupid. This is how I learn.
AtomicPtr doesn't (and can't) make mutation behind the pointer atomic. The only thing it does is allow you to manipulate the pointer itself atomically.
You might be looking for Atomic. In theory there is no guarantee that this type is in fact lock free; in practice it should be lock free for any struct whose size is no larger than the largest atomic primitive on your platform.
Stumbled upon this crate and looked inside the code before. Correct me if I'm wrong, but it falls back to a spinlock if the type isn't primitive. That's a bit of a bummer.