3 ms·
Out of curiosity, what's the difference between virtual threads, green threads and, say, goroutines?
by Yoric 4y ago
Out of curiosity, what's the difference between virtual threads, green threads and, say, goroutines?
- MrBuddyCasino 4y agoIts solving the function colouring issue. Your code can run against old-school OS-threads with blocking I/O, or it can run against virtual threads with non-blocking I/O that look and behave almost exactly the same, but are multiplexed onto fewer underlying OS threads, without needing to introduce language constructs such as async/await or changing anything in your program code except maybe the executor implementation. Golang is opinionated towards its single coroutine model, there is no way to switch. Its a good model and it works well, but there are cases where it leaks, such as low-performance C interop. Green threads usually refer to single-threaded runtimes such as Node.
- butt__hugger 4y ago[dead]
- Yoric 4y agoSo it's basically an implementation of transparent of M:N scheduling, right? I remember that early versions of Rust had that but dropped it because building a single implementation of that that scales to all platforms is hard (tm).
- nu11ptr 4y agoYep, M:N cooperatively scheduled. It also requires a "runtime" which Rust didn't want to have
- ithrow 4y agoGreen threads usually refer to single-threaded runtimes such as Node. This doesn't sound right. How is the single-threaded event-loop of node green threads?
- MrBuddyCasino 4y agoYou're right, that was a bad example.
- samus 4y agoApart from that, it makes a lot of sense to have multiple threads in Node. They just run in the background, do stuff, and send events into JS land from time to time.
- twic 4y agoNot a lot. To a large extent, they are different names for the same concept. But since they are the names used for that concept by specific implementations, they also imply the particularities of those implementations. The old Java green threads could only use a single core, the new Java virtual threads can use multiple cores. AFAIK, Java green and virtual threads are both cooperatively scheduled, Goroutines are, nowadays, preemptively scheduled. And so on and so forth.
- kaba0 4y agoJava's virtual threads are preemptive, not cooperative -- they are preempted not at explicit points, but at blocking IO operations.
- twic 4y ago> they are preempted not at explicit points, but at blocking IO operations. That is cooperative scheduling.
- diroussel 4y agoIf you can’t break a thread out of an infinite loop, that is pure cpu and no io, then the run time is not preemptive, surely??
- kaba0 4y agoAt worst the OS will preempt the thread and the JVM will likely schedule a different virt thread the next time — but in case you have multiple CPU-hungry virtual threads fighting over a single core it will suck no matter what. The performance improvement comes from doing useful work on a thread during IO waits. Nonetheless, there were discussions about some way to force preemption at certain points, not sure whether there is a decision since, but that will be the exception, not the rule.
- Yoric 4y agoStill trying to understand: what's the difference between "forc[ing] preemption" and cooperative threading?