Achieving Thread-Safety

后端 未结 5 901
天命终不由人
天命终不由人 2020-12-23 10:58

Question How can I make sure my application is thread-safe? Are their any common practices, testing methods, things to avoid, things to look for?

5条回答
  •  忘掉有多难
    2020-12-23 11:02

    My simple answer combined with those answer is:

    • Create your application/program using thread safety manner
    • Avoid using public static variable in all places

    Therefore it usually fall into this habit/practice easily but it needs some time to get used to:

    program your logic (not the UI) in functional programming language such as F# or even using Scheme or Haskell. Also functional programming promotes thread safety practice while it also warns us to always code towards purity in functional programming. If you use F#, there's also clear distinction about using mutable or immutable objects such as variables.


    Since method (or simply functions) is a first class citizen in F# and Haskell, then the code you write will also have more disciplined toward less mutable state.

    Also using the lazy evaluation style that usually can be found in these functional languages, you can be sure that your program is safe fromside effects, and you'll also realize that if your code needs effects, you have to clearly define it. IF side effects are taken into considerations, then your code will be ready to take advantage of composability within components in your codes and the multicore programming.

提交回复
热议问题