5 ms·
(If it's not clear you need to navigate the presentation with both left/right and down keys) You know what? This is getting pathological. It seems keywords are
by raverbashing 11y ago
(If it's not clear you need to navigate the presentation with both left/right and down keys)
You know what? This is getting pathological. It seems keywords are added to ensure serialization/atomicity, then compilers find a way to optimize it away/make it useless, then another thing "now it's the right way" gets added. Rinse, repeat
And most things that are added have some quirks and people trying to play it smart
- the8472 11y agoWe already have volatile for "yes, please do this as two separate increments, don't coalesce them". And atomics give you ordering guarantees and mappings to atomic instructions. And fence methods give you full fences where needed. So we already have all those things you might need, they just got decomposed into different aspects that can be applied separately.
- masklinn 11y agoIf you use <space> it'll go to the next slide, whether down or to the side.
- sjolsen 11y ago>It seems keywords are added to ensure serialization/atomicity, then compilers find a way to optimize it away/make it useless No, what happens is keywords (or more broadly, semantics) are added which provide certain guarantees given certain preconditions. Then, developers write code that depends on those guarantees and fails to meet the preconditions, but happens to work anyway. At some point, the implementation adds some optimization which -- while still providing the appropriate guarantees when the preconditions are met -- breaks that code. I know C and C++ can be a royal pain in the ass sometimes, but I have little sympathy for developers who flip the "I know what I'm doing, please assume my code is right and make it run fast" switch, then get upset with the compiler when it turns out that their code actually _isn't_ right and the compiler performs an optimization they weren't expecting. If you don't want the compiler to reorder your memory access, then don't fuck around with the memory_order flags.
- raverbashing 11y ago> Then, developers write code that depends on those guarantees and fails to meet the preconditions, but happens to work anyway. This reminds me of the memcpy/memmove issue. You should use "move" when areas overlap, not memcpy The real question is: why are there two versions? Checking if the memory overlap is very cheap compared to copying, let's say 16 elements. And if you're memcpy'ing smaller sizes you can probably do it manually > I know C and C++ can be a royal pain in the ass sometime Yes, they lack enough information to know what you are actually trying to do and have to guess a lot of things. Not sure how "C++ smart" are modern compilers. (Example: you pass an object by value, and only read one field, can it optimize this?)
- david-given 11y agoI see they've finally changed the memcpy() specification to forbid overlapping memory areas completely. Previously, it was defined to copy from low addresses to high addresses, which meant you could use this to fill an array: p[0] = 42; memcpy(&p[1], &p[0], sizeof(p)-sizeof(*p)); And people did.
- caf 11y agoThe original 1989 ANSI C specification stated, in "4.11.2.1 The memcpy function": If copying takes place between objects that overlap, the behavior is undefined. So it has been this way in C since the first official standard. I don't have a first edition K&R so I can't see what that said, though.
- david-given 11y agoHuh. You're quite right. Well, it may have been undefined, but in practice the behaviour was standardised and couldn't be changed without breaking existing code... which is basically C in a nutshell.
- caf 11y ago