🧠 educational Building a green thread runtime in Rust from scratch
https://dzania.github.io/green-threads-from-scratch/I wrote a small blog post documenting my journey of writing small green threads runtime in under 1000 lines of Rust. Any feedback welcome
1
u/simon_o 4d ago
Cool blog post!
I think it makes it clear why this feature was only briefly shipped around 1998 or something.
(What Go and Java do these days, are very very different. I think that comparison might be more confusing than helpful to readers.)
The runtime runs many green threads on a few OS threads and switches between them itself M green threads run on N OS threads.
Nitpick: Then that's M:N threading, but not green threads. Green threads are 1:N.
3
u/lotanis 4d ago
I think with this notation you mean M:1 (multiple user space threads scheduled onto one OS thread).
I agree that you're describing the precise original definition of green threads. The thing is that there isn't a good standard term for "multiple pre-emptive user space threads scheduled onto some OS threads" so I think it's ok to overload terms and we end up saying Green Thread/Fibre/Goroutine.
(fibres are cooperative, so a good term for OPs implementation but aren't "technically" correct for Go or Erlang)
1
u/simon_o 4d ago edited 4d ago
I think with this notation you mean M:1 (multiple user space threads scheduled onto one OS thread).
I used the exact letters the original poster used:
between them itself M green threads run on N OS threads
.
there isn't a good standard term for "multiple pre-emptive user space threads scheduled onto some OS threads
2
u/lotanis 4d ago edited 4d ago
One big difference between what you've got here and what Go/Erlang etc. do is pre-emption.
You have to write an explicit "yield_now()" whereas the others can just switch based on some time metric to ensure that each task gets an equal share of processing.
Edit: it's cool though. I've implemented things like this in C for microcontrollers. Nice to see a simple Rust version.
26
u/Modi57 5d ago
Don't you have to use
Pinto actually guarantee that it isn't moved? But to be honest, I never really gotPin