But there is also a tree of threads!
It is important to always keep BOTH mental models in your head when writing C++ code. Just like you need to track which object owns which other object (for freeing memory and avoiding memory leaks), you ened to track which thread is executing each line of code (for avoiding race conditions). This is a very common error in c++ programming, and several patterns have sprung up to avoid having to think about it. Debugging race conditions is very difficult because they are non-deterministic (they may not occur on every execution of the program, making them hard to test repeatably).
Patterns for avoiding thinking about threading in c++ include
- a god object with locks (the worst option, not really multi-threaded)
- message passing (erlang, golang channels, "event-driven" frameworks)
- immutable objects (functional languages)
No comments:
Post a Comment