2 ms·
Q: How do you do atomic unaligned accesses from C++? (and I'm sure someone will say "align your data". But that's not always possible.) Edit: I mean on x86, wh
by dataflow 3y ago
Q: How do you do atomic unaligned accesses from C++? (and I'm sure someone will say "align your data". But that's not always possible.)
Edit: I mean on x86, where it's possible. I couldn't even find any intrinsics for the corresponding lock instructions. And yes I'm aware of the performance penalty.
- cjensen 3y agoPortably? You don't. That's a CPU limitation for some architectures.
- cesarb 3y agoAFAIK, in general, it's not possible; it's a hardware limitation of the CPU. Some architectures like the x86 do allow unaligned atomic accesses, but in a very heavy handed way (doing an unaligned atomic access on the x86 locks the whole bus, stalling all processor cores at the same time). The Linux kernel recently gained the ability to detect this situation and, depending on configuration by the administrator, it could either log a kernel warning or kill the offending process. Some discussion about this on LWN found on a quick web search: https://lwn.net/Articles/790464/ https://lwn.net/Articles/790464/ https://lwn.net/Articles/806466/ https://lwn.net/Articles/806466/ https://lwn.net/Articles/911219/ https://lwn.net/Articles/911219/
- deleted 3y ago[deleted]
- JonChesterfield 3y agoC++ cannot represent an atomic type with less than natural alignment, and you cannot access that which does not exist, so that's the end of your road. Consider a language with lower intrinsic overhead. Usually the answer is to ignore the atomic abstractions of C++ and use the GCC style atomic intrinsics (they take a void* instead of some atomic qualified thing) but I'm not totally confident they'll do the right thing if the target memory has less than natural alignment. Beware crossing cache lines with your operation. I'm not sure what the x64 instructions do in such a case.
- dataflow 3y ago> Usually the answer is to ignore the atomic abstractions of C++ and use the GCC style atomic intrinsics I'm saying I don't see any such intrinsics for this. I do believe x86 and x64 LOCK works correctly across cache lines.