How to define threadsafe?

后端 未结 8 1970
夕颜
夕颜 2020-12-31 11:48

Threadsafe is a term that is thrown around documentation, however there is seldom an explanation of what it means, especially in a language that is understandab

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 12:12

    Multithreading leads to non-deterministic execution - You don't know exactly when a certain piece of parallel code is run.

    Given that, this wonderful multithreading tutorial defines thread safety like this:

    Thread-safe code is code which has no indeterminacy in the face of any multithreading scenario. Thread-safety is achieved primarily with locking, and by reducing the possibilities for interaction between threads.

    This means no matter how the threads are run in particular, the behaviour is always well-defined (and therefore free from race conditions).

提交回复
热议问题