Sometime we really need to choose a multi-threaded design, because single-threading will block the UI or block another thread.
But sometimes using multiple threads is just one of the choices, for example, you may able to replace multi-threading with timer. In that situation, you may want to avoid the effort of investigating the performance risk of using timer and directly go to the multi-threaded design. But as we know, threading has a lot of drawbacks:
- Sync issue.
- Debugging is tough for sync issue.
- Deadlock issue.
The #1 can always resolved by adding locks, and #3 is the hardest to resolve, it may need design level changes. So let's focus on when will be safe to choose multi-threaded design without having deadlock issues.
I use the Following rules to judge it is safe or not:
- The component only has input data and entry function call interaction.
- Or the component only has output data and exit function call interaction.
- Or you have entry and exit data or function call, but you can make sure all the data that goes in can be handled asynchronously. (I cannot think out of case, because you almost always need to syncronized to push the data into an queue.)
Does that make sense to you, any thought is welcome.