Glossary · Automation software engineering and architecture
Thread safety
Also known as: Thread-safe code
German: Thread-Sicherheit
In concurrent programming, thread safety is the property of code or data structures to behave correctly when accessed by several threads at the same time, without race conditions or corrupted state.
- Software engineering
In one sentence
Thread safety means code or data structures behave correctly when several threads use them at the same time.
Example
The alarm list class is thread-safe: the communication thread can add alarms while the HMI thread reads the list, because both use the same lock.
How it applies
- Engineering: Thread safety is achieved through synchronization, immutable data, thread-local data or by confining data to one thread. Each approach has costs in performance or design.
- Verification: Thread safety is hard to prove by testing. Code review with explicit threading assumptions, static analysis and stress tests are used together.
- Documentation: API references should state for each class or function whether it is thread-safe. Missing information leads integrators to guess, which causes intermittent field failures.
Thread safety vs. safety
Thread safety concerns software correctness under concurrency. It has nothing to do with functional safety or machine safety, although thread-safety defects can affect safety-related software. Use the full term to avoid confusion.