6 ms·
Low-level thread programming is a minefield. I've been doing IRQ/concurrent programming since the 1980s, and still hate it. Thread bugs are a nightmare. Concur
by ChrisMarshallNY 4y ago
Low-level thread programming is a minefield. I've been doing IRQ/concurrent programming since the 1980s, and still hate it. Thread bugs are a nightmare.
Concurrency needs to be "baked into" higher-level languages, development APIs, and standard libraries. Basically, covered in bubble-wrap. That's starting to happen. It won't be as efficient as low-level coding, but it is likely to still have a significant impact on the performance of most code.
And folks that are good at low-level threading would be well-served to work to support this.
- nick_ 4y agoPony has concurrency baked in via high-perf actor implementation. It's really nice. I believe Go has this as well, but it also has low-level concurrency API as well?
- skohan 4y agoIdk I think it is not as difficult as it is sometimes framed. I think the key is to design your program/separation of concerns in such a way as to make it easy to reason about concurrency, and minimize the need for synchronization points. I think a lot of the problems arise when you just try to write normal synchronous code, or to parallelize code which was originally synchronous, and don't realize the implicit constraints you had been relying on which no longer hold when concurrency is introduced.
- ChrisMarshallNY 4y agoFP is good for adapting to threading, but it has difficulties, when applied to things like GUI programming, async communications, or device control (places that need threading). A lot of languages are getting things like async/await/actor, etc., but even that is still too low-level for a lot of programmers. It can easily turn into a lot of single-threaded code. It needs to be completely under the surface. Swift does that with generics (I suspect other languages do it, as well). For example, you can say Array<Int>, or [Int]. They mean the same thing, but one does not have the generic syntax. If we can do similar stuff with things like mutexes and syncing, then it will go a long way towards highly performant, safe, code.
- skohan 4y agoIt's interesting that you choose Swift as an example, because I think in some ways Swift does a poor job of accommodating async using the "bubble wrap" approach. Specifically I am talking about the default of using atomic reference counting on all references. This makes it somewhat idiot proof at least with respect to avoiding NPE's, but it comes at a huge performance cost (especially on x86 which I guess is becoming less of a concern for Swift code). I think this is a fundamental issue: a big part of the benefit of concurrency and parallelism is supposed to be increased performance. However a lot of the ways to make concurrency "generally safe" tend to hamstring performance, because you have to protect against all the different types od errors programmers can make, which basically means a lot of expensive synchronization. Maybe there is a way to hide this completely, as you say, in a performant way. To do this, I think you would almost have to have either some kind of really clever risk-free data structures, or else a very smart compiler. Another approach might be to keep concurrency at arms length from the average programmer. I.e. hide 90% of it inside of libraries, and expose safe interfaces which the programmer interacts with in a mostly synchronous way.
- ChrisMarshallNY 4y ago> I think in some ways Swift does a poor job of accommodating async using the "bubble wrap" approach. I agree. Async/await has a "kludgy" feel to it (to me). It's "added on," not "embedded in." But I'm not a language writer, so I don't have alternatives to offer. I was talking about how it does generics. I think it does that quite well.
- samsquire 4y ago
- hinkley 4y agoBased on a non-scientific study, I think the spatial thinkers do great with concurrency, the visual thinkers do okay, and everyone else is in real trouble. Which reminds me, I need to interview my developer friend with aphantasia about how he feels about concurrency. Concurrency should be the sizzle and not the steak, otherwise you're reducing the diversity of your team rather substantially. Good people are hard enough to find. Driving the people you have away doesn't generally end well.
- deleted 4y ago[deleted]
- toast0 4y agoI wouldn't say I have aphantasia, but I don't know that I think of computers in a visual way, although kind of maybe. I'm not great at writing concurrent code with locks and shared memory (messaging passing is much easier for me to do well; but message passing is almost exclusively built upon locks and shared memory, so my hands are forced). But I'm pretty good at debugging concurrency problems of either type. It's all about thinking about the sequence of events of an individual thread of execution and not making assumptions that things would necessarily stay the same between steps... and then trying to line up how another thread could make those things happen. I don't line the threads up visually, but I guess you could?
- hinkley 4y agoI studied distributed computing in college, and I spent a lot of time not quite internalizing the fact that since this was an elective at one of the highest ranked state schools in the nation, probably most other people didn't have the same information I did. I ended up doing a lot of concurrency work early on because I was good at it, but over time the evidence just kept piling up that nobody else could really follow it, and so I've used it less and less over time, and in particular try very hard to keep it away from the main flow of the application. It's more something I pull out for special occasions, or to explain bugs. Where a lot of frameworks fail is that while re-entrance and concurrency are different problems, they share a lot of qualities, both computationally and with the limits of human reasoning. Recursive code with side effects looks and acts a lot like concurrent code, because here I am looking at some piece of data and the moment I look away some other asshole changed it out from under me. Most frameworks end up being a bad study in recursion, usually in the name of reducing code duplication. Pure functional people love recursive code, but it's the pure part that avoids the problems with trying to interlace multiple activities at once. Without that, you're trying to eat your dessert without eating your vegetables first.
- deleted 4y ago[deleted]
- nyanpasu64 4y agoIn audio code, I'd rather use a properly written wait-free SPSC queue, than a least-common-denominator messaging mechanism provided by the standard library like postMessage() (where both the Win32 and JavaScript version suffer from contention and cause audio stuttering, see https://github.com/Dn-Programming-Core-Management/Dn-FamiTracker/pull/137 https://github.com/Dn-Programming-Core-Management/Dn-FamiTra... and https://blog.paul.cx/post/a-wait-free-spsc-ringbuffer-for-the-web/ https://blog.paul.cx/post/a-wait-free-spsc-ringbuffer-for-th...), though I'm not sure if generic channel/queue objects are as bad in practice. And message-passing (with anything other than primitive types) is a pattern for sharing memory that, if properly implemented and utilized (you don't send a pointer through a channel and access from both threads/goroutines), ensures no more than 1 thread accesses the object in a message at a time. I think most but not all code can be constructed using primitives like (regular or wait-free) message queues and (RwLock or triple buffer) value cells, but I think all concurrent code which communicates with other threads of execution needs concurrent reasoning to design and write correctly. In my experience, Rust marking data as exclusive or shared is quite helpful for concurrent design and reasoning, whereas prohibiting shared memory altogether reduces performance drastically but is no better at correctness. I think message-passing merely shifts data race conditions into messaging race conditions (but perhaps Go is easier to reason about in practice than I expect). In fact, building programs heavily reliant on message passing between separate OS processes per service (like PipeWire) doesn't suffer from multithreading but rather multiprocessing and distributed systems, making it harder to establish consistent execution states or data snapshots at any point in time, or reason about invariants. And even code not intended to communicate between threads needs to take care that no state is shared and mutated by another thread on accident (I concede this is easier with JS Web Workers or Rust which restrict shared mutability, than C++, Java, or Go which don't).
- samsquire 4y agoGo has a number of problems with regard to data races https://eng.uber.com/data-race-patterns-in-go/ https://eng.uber.com/data-race-patterns-in-go/ I think you need shared memory concurrency for performance. There are papers that argue for optimistic concurrency control and blocking concurrency control. Concurrency Control Performance Modelling Alternatives and Implications