6 ms·
I don't want to say you should never do this. But if you aren't writing a compiler or an embedded system, you should never do this.
by powera 3y ago
I don't want to say you should never do this.
But if you aren't writing a compiler or an embedded system, you should never do this.
- spacechild1 3y agoIt is also very useful for some lockfree algorithms, e.g. to fit a pointer and a sequence number within a 64-bit value.
- teh_matt 3y agoYou should be very careful when doing that. It may be rare in practice, but it's surprisingly easy to trigger ABA issues with only a 16-bit sequence number.
- ot 3y agoDon't you need 65k threads all contending on the same state for that to happen? Even if your process does have 65k threads, you'd need a pretty large critical section for all of them to be preempted in an unlucky point. That said, it's better to rely on RCU/hazptr to solve ABA issues, but the extra bits are still useful to store state that can be CAS'd together with the pointer.
- ot 3y agoCannot edit anymore but I realized that my comment was wrong :) You just need 2 threads, as long as one is suspended long enough.
- deleted 3y ago[deleted]
- spacechild1 3y agoI don't really believe that it is easy to trigger. The thread would have to be preempted at a very specific point for a very specific duration, the other thread(s) would need to perform exactly 2^16 operations within that time window and the final operation would need to trigger the ABA problem. Possible, but extremely unlikely. (In some applications even impossible.) Do you happen to have some real world examples? But yes, it is definitely something to be aware of! BTW, if the pointers are always aligned, the sequence number may have a few bits more.
- deleted 3y ago[deleted]
- JonChesterfield 3y agoDynamic language runtimes really like this. It gives you type information without dereferencing the pointer.
- KolmogorovComp 3y agoAny graph-heavy application constrained by memory potentially gain a lot from those kind of optimizations.
- UncleEntity 3y ago> But if you aren't writing a compiler or an embedded system, you should never do this. I have this hex trie library that uses this to differentiate between a leaf and a node that will soon be optimizing single-value nodes as tagged pointers. __edit__ umm... single-value nodes are leaves!?! Yeah, not enough coffee apparently. Not to mention the tinyscheme interpreter I occasionally poke at that TFA gave me a bunch of ideas to try out.