What is the meaning of the term “Thread-Safe”?

Technology CommunityCategory: ConcurrencyWhat is the meaning of the term “Thread-Safe”?
VietMX Staff asked 3 years ago
Problem

Does it mean that two threads can’t change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when multiple threads are executing that code segment?

  • Thread-safe code is code that will work even if many Threads are executing it simultaneously within the same process. This often means, that internal data-structures or operations that should run uninterrupted are protected against different modifications at the same time.
  • Another definition may be like – a class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronisation or other coordination on the part of the calling code.