Garbage Collection and Concurrency
It was not always obvious to me, but Concurrency and Garbage Collection are like PB+J, or french fries and ketchup; they make a kick-ass pair!
In non-GC languages and a concurrent piece of code, it’s not really straightforward when the last thread has finished using some resource so it can be released; this might mean performing manual book-keeping, or using specific data-structures and patterns which ensure proper cleanup.
On the other hand, GC languages are able to circumvent this whole class of bugs abstracting it from the programmer’s view. Even the trivial case of using some background thread to perform a periodic action is simplified.
Of course, both non-GC languages have been keeping up, with innovations like smart pointers, and GC languages try to innovate with simpler and faster collectors, but the point still stands!
Writing correct concurrency in Go can be easier, than for example in C++ where you’d have to think about things such as RAII.
Disclaimer : My C++ knowledge is sketchy at best. I’ve only used it for academic work narrowly centered around a framework, either Fluka, OpenMP or Mad-X.This post included some examples to showcase this issue, but they were not up to par, so they were removed, until I find some time to practice my C++ and write some new ones.
Parting Words
I’m still looking for a better way to explain this concept with an example. Feel free to reach out if you have any good ideas!
I also recommend watching MIT’s 6.824 class on distributed systems where this issue was mentioned, if you’re into this kind of stuff.
Some Resources
- https://www.modernescpp.com/index.php/garbage-collectio-no-thanks
- https://www.modernescpp.com/index.php/c-core-guidelines-sharing-data-between-threads
- https://www.modernescpp.com/index.php/c-core-guidelines-concurrency-and-lock-free-programming
- https://codereview.stackexchange.com/questions/212101/automatic-raii-wrapper-for-concurrent-access
- https://stackoverflow.com/questions/22842579/best-way-to-handle-multi-thread-cleanup
- https://www.c-sharpcorner.com/article/programming-concurrency-in-cpp-part-1/
- https://en.wikitolearn.org/index.php?title=Special:Book&bookcmd=download&collection_id=1fb90160b4f2d638c772ab72e3532c3e6967246a&writer=rdf2latex&return_to=Project%3ABooks%2FConcurrent+Programming+in+CPP
- https://docs.microsoft.com/en-us/cpp/cpp/smart-pointers-modern-cpp?view=vs-2019