How to define threadsafe?

后端 未结 8 1971
夕颜
夕颜 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:14

    A thread safe function / object (hereafter referred to as an object) is an object which is designed to support multiple concurrent calls. This can be achieved by serialization of the parallel requests or some sort of support for intertwined calls.

    Essentially, if the object safely supports concurrent requests (from multiple threads), it is thread safe. If it is not thread safe, multiple concurrent calls could corrupt its state.

    Consider a log book in a hotel. If a person is writing in the book and another person comes along and starts to concurrently write his message, the end result will be a mix of both messages. This can also be demonstrated by several threads writing to an output stream.

提交回复
热议问题