5 ms·
This addresses the wrong problem. The real issue is control over data shared between threads, not control flow. C/POSIX type threads have no language support f
by Animats 9y ago
This addresses the wrong problem. The real issue is control over data shared between threads, not control flow.
C/POSIX type threads have no language support for indicating what data is shared and which locks protect which data. That's a common cause of trouble. The big question in shared memory concurrency is "who locks what". Most of the bugs in concurrent programs come from ambiguities over that question.
Early attempts to deal with this at the language level included Modula's "monitors", the "rendezvous" in Ada, and Java "synchronized" classes. These all bound the data and its lock together. Rust's locking system does this, and is probably the most successful one so far. (Yes, the functional crowd has their own approaches.)
Go talked a lot about controlling shared memory use. The trouble with goroutines, as Go programmers found out the hard way, was that the "share by communicating, not by sharing" line was bogus. Even the original examples had shared data. But the language didn't provide much support for controlling that sharing.
Python is basically at the C level of sharing control over data, except that the Global Interpreter Lock keeps the low-level data structures from breaking. This prevents Python programs from doing much with multi-core CPUs. Since this is just another thread library for Python, it has the same limitations.
Real concurrency in Python with disjoint data, and without launching a heavy-weight subprocess, would be a big win. But this isn't it.
- nnq 9y ago> Even the original examples had shared data. But the language didn't provide much support for controlling that sharing. Can you elaborate on this?
- Animats 9y agoYes, but there will be a service charge.
- rumcajz 9y agoAlmost all attempts at CSP-style programming in the end resorted to sharing data to get a little bit better performance. I wonder whether we shouldn't have used a bit of speedup that Moore's law gave us to cover that cost and be done with all the shared state headaches.
- kllrnohj 9y agoExcept shared data doesn't give you a little bit better performance, it gives you massively better performance. Or, in some cases, it's the only way to get usable performance at all. Now what you could do is break objects down into annotated types. Consider immutable vs. mutable in combination with thread-unsafe, thread-compatible, and thread-safe. Immutable data that's not thread-unsafe you can share freely across threads, all is well. L2/L3 caches are happy. Mutable that's thread-safe can similarly be shared at will. Then you can force that thread-compatible objects be wrapped & accessed only from a Mutex or transfered between threads as part of a move operation. Rust gives you the tools to do all of this, and indeed does some of it, but as part of the steep learning curve of the ownership model.
- Animats 9y agoI proposed something like that for Python in 2010.[1] Immutable objects could be shared. Mutable objects had to either be unshared, or a subclass of an object that enforced synchronization. Python's Little Tin God didn't like it. Mostly because I proposed to freeze the code of the program once the second thread started. That takes away much of the dynamism he insists on. Might be worth looking at again. The separation of data into immutable, unshared, or synchronized is mainstream now. [1] http://animats.com/papers/languages/pythonconcurrency.html http://animats.com/papers/languages/pythonconcurrency.html
- kllrnohj 9y agoPython seems like an odd place to try and shove this into. Both due to its heavy object mutability & dynamic nature in combination with the GIL making heavy threading of python code a waste of everyone's time anyway. There aren't many languages with the concept of ownership or moving at all, and trying to retro-fit that is probably going to be not a good experience for anyone involved. Rust is largely there, in yet another thing it does well, but if you don't want that something like C++ would be probably a better place to try it. There you at least have move & ownership as a language concept already.
- yxhuvud 9y agoI think threads is a big enough of a problem that it can be divided into multiple sub problems where each individual problem deserves a solution. Yes, the resource sharing problem is harder than the part that this solves, but does that really matter?
- Animats 9y agoYes, because control flow and shared data locking work together. Or should.
- yxhuvud 9y agoBut isn't it the case that while nurseries may not solve shared data handling, it certainly doesn't make it harder to reason about. If you get a fix rule that when the program has passed a certain point (the nursery) then the threads are done and the shared data related to the task will not be shared anymore.