I've always been Mr Sustrik's fan, but this has a special place in my heart.
I highly recommend reading the "What is structured concurrency?" section with understanding. I think this is a big deal.
Why?
Let's start from another angle. Have you ever seen `pthread_cancel` manpage? Or "POSIX.1 Cancellation points"? This is an utter mess!
More than that, _any_ framework I've seen that allows to "kill" a coroutine (goroutine, thread, actor) in the end reproduces this "pthread_cancel" drama.
Basically, thread cancellation seems to be an unsolved computer science problem.
Mr Sustrik in "structured concurrency" makes one easy assumption, which in practice is sensible. It's not academic proposal. It's totally useable. The programmer must understand it and use it properly, but this is a totally workable constraint - that is:
- the thread must exit before its parent dies.
This small thing seems to be making all the differnce.
Anyhow. If you ever wondered why killing threads is a mess, libdill proposes a solution.
Might be fun to play with, but I wouldn't rely on it. Generally, better off with libuv for existing projects or Rust for greener fields where lifetimes are checked and safe concurrency is much easier.
I'm just going to leave this here, don't shoot the messenger, I already had a bad day of downvoting:
"Many lock-free structures offer atomic-free read paths, notably concurrent containers in garbage collected languages, such as ConcurrentHashMap in Java. Languages without garbage collection have fewer straightforward options, mostly because safe memory reclamation is a hard problem..." - Travis Downs https://travisdowns.github.io/blog/2020/07/06/concurrency-co...
In my experience, it's easier to write code that is resilient to hiccups in C than in Java. Solving SMR with GC only offers something close to lock-freedom when you can guarantee global GC pauses are short enough... and common techniques to bound pauses, like explicitly managed object pools, land you back in the same problem space as C.
Maybe, but Java is the most successful server side language because it has other features that C lacks and will always lack, the most important being: it does not seg. fault on you!
It gives you a stack trace that helps you debug in seconds and when that is not enough a heap dump gives you enough information to solve the problem of a live service without having it down.
To use any other language for servers is madness. For clients I prefer C (with a tad of C++ for convenience like namespaces, string and streams).
I even hot-deploy to it with .dll/.so like I hot-deploy the classloader to Java for development speed!
Another quote you'll try to deconstruct but I know it to be true because I use it every day:
"While I'm on the topic of concurrency I should mention my far too brief chat with Doug Lea. He commented that multi-threaded Java these days far outperforms C, due to the memory management and a garbage collector. If I recall correctly he said "only 12 times faster than C means you haven't started optimizing"." - Martin Fowler https://martinfowler.com/bliki/OOPSLA2005.html
It mostly follows the Kotlin model of structured concurrency. However it has its sets of drawbacks due to Rust Futures being immediately cancellable, which then leads to problem if a parent task gets simply dropped.
See Ada's tasks for something approaching this concept, though it's not strict. That is, it's still possible to have "unstructured" concurrency in Ada. But tasks and instances of task types have lexical scope like any other variable, by default. I started exploring it a while ago but my interest kind of petered out so I never put anything together to really present on it.
I highly recommend reading the "What is structured concurrency?" section with understanding. I think this is a big deal.
Why?
Let's start from another angle. Have you ever seen `pthread_cancel` manpage? Or "POSIX.1 Cancellation points"? This is an utter mess!
More than that, _any_ framework I've seen that allows to "kill" a coroutine (goroutine, thread, actor) in the end reproduces this "pthread_cancel" drama.
Basically, thread cancellation seems to be an unsolved computer science problem.
Mr Sustrik in "structured concurrency" makes one easy assumption, which in practice is sensible. It's not academic proposal. It's totally useable. The programmer must understand it and use it properly, but this is a totally workable constraint - that is: - the thread must exit before its parent dies.
This small thing seems to be making all the differnce.
Anyhow. If you ever wondered why killing threads is a mess, libdill proposes a solution.