4 ms·
C has never been "how the computer works", its abstract model is way too… well, abstract. But there is an interesting catch to it: because C is so important, a
by Tomte 7y ago
C has never been "how the computer works", its abstract model is way too… well, abstract.
But there is an interesting catch to it: because C is so important, and programmers at large know so little "real C", vendors do their damndest to make it work for them.
A well-known microcontroller forum in Germany is constantly pushing that you need to make your code safe in the case of concurrent access to a variable by a main loop and an interrupt. By making it volatile.
Strictly speaking that's plain wrong. You need some kind of mutex.
Practically speaking, no embedded C compiler vendor would dare use an "optimizing opportunity" and surprise his customers there.
- icedchai 7y agoYes, except it's "less abstract" than any other language in mainstream use...
- Tomte 7y agoYou're right. But that wasn't my point. My point was that C isn't the close-to-the-machine language that many programmers think it is. That other languages don't qualify, either, is beside the point. Ruby or Java aren't mistaken to be close-to-metal. Point in case: you don't even see anything about Harvard vs. von Neumann architecture in portable C sources. Not to speak of micro code or anything like that.
- icedchai 7y agoRuby and Java aren't mistaken to be close-to-metal because they aren't. C is primarily about memory access and structure at a relatively low level. How do you explain that most operating system kernels and device drivers are written in C? Again, C is much closer to the machine than anything else in mainstream use. The C "abstract machine" is mostly a myth.
- Tomte 7y agoAgain, I don't dispute that C is much closer to the metal than Java. I'm disputing that the C execution model is adequately and exhaustively modelling the idiosyncracies of real existing modern hardware, where "modern" means "almost anything in my lifetime".
- takeda 7y ago> You're right. But that wasn't my point. My point was that C isn't the close-to-the-machine language that many programmers think it is. It is, but what happened though, is that assembly/machine language no longer is as close to the real machine as it was in the past, it is an abstraction on top of micro code. The high level languages that you mentioned are further away, and competition abstracted away from the assembly.
- scottLobster 7y ago"Concurrent access" isn't actually an accurate description of what's going on. Microcontrollers are generally single-core, single-threaded devices. The most common pipeline goes main thread -> interrupt processing -> resume main thread. If you're modifying a variable in the interrupt you should declare it volatile so as to avoid the compiler optimizing it and introducing unexpected behavior. Adding a mutex would just be worthless overhead.
- Tomte 7y agoThe C language until very recently didn't know about threads or other kinds of concurrency. So the compiler was technically free to assume that the interrupt is never called. It's nowhere in the call tree that originates at main(). Of course, it didn't do that. Because vendors imparted their compilers with additional knowledge about common programming patterns and uses that aren't standard C.
- scottLobster 7y agoSo there are vendors out there that alter the behavior of volatile to act like a mutex in multi-threaded environments but don't tell anyone? Ugh. Well, TI doesn't do that thankfully. At least not for the MSP430.
- dirtydroog 7y agoHow did you draw that conclusion? People have been warning about volatile for years
- gpderetta 7y agofor a while Microsoft gave volatile acquire/release semantics, which implied a barrier on non-x86 cpus. They recognized it was a mistake backtracked on that around the time they started their effort to run Windows on ARM.