3 ms·
I’m confused about the assertion that green threads perform badly. 3 of the top platforms for high concurrency servers use or plan to use green threads (Go, Erl
by nsm 1y ago
I’m confused about the assertion that green threads perform badly. 3 of the top platforms for high concurrency servers use or plan to use green threads (Go, Erlang, Java). My understanding was that green threads have limitations with C FFI which is why lower level languages don’t use them (Rust). Rust may also have performance concerns since it has other constraints to deal with.
- yxhuvud 1y agoGreen threads have issues with C FFI mostly due to not being able to preempt execution, when the C thing is doing something that blocks. This is a problem when you have one global pool of threads that execute everything. To get around it you essentially need to set up a dedicated thread pool to handle those c calls. Which may be fine - go doesn't let the user directly create thread pools directly but do create one under the hood for ffi interaction.
- layer8 1y agoThe problem is when C calls expect to be on a particular thread. Either the main event thread, in a GUI context, or the same thread as some related previous call.
- ori_b 1y agoThe problem is that C expects to have enough stack to put stuff there, but green threads allocate small stacks to reduce (virtual) memory use.
- layer8 1y agoThat’s not a problem when a dedicated thread pool is used as mentioned by GP. However they don’t solve the thread affinity issue.
- ori_b 1y agoThat's the reason for the thread pool, and the resulting thread affinity issue.
- layer8 1y agoThis seems to assume that the same green thread will always run on the same native thread, which I don’t think is universally the case.
- yxhuvud 1y agoDifferent languages give different level of control over that. There are languages with one main thread pool and perhaps some specialized ones that users don't have control over. Go would be an example of this. It is also possible for languages to make user creatable thread pools - possibly even with affinity to cores, allowing fibers to run only on a single thread. Crystal is coming along that path. So far it seems to be coming around fairly nicely but I havn't had to battle the GC in anger yet.
- ori_b 1y agoI don't understand why that matters.