Are Delphi simple types thread safe?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Delphi, simple types like Integer, Boolean, pointers, and enums are not automatically thread-safe just because they are primitive. Thread safety depends on operation semantics, CPU architecture, and memory ordering. A single aligned read/write may be atomic on many platforms, but compound operations such as increment and check-then-set are not atomic without synchronization.
To write correct concurrent Delphi code, treat shared state as unsafe by default and apply explicit synchronization or atomic primitives.
Core Sections
1. Atomic read/write is not enough
Even if an assignment is atomic, race conditions still happen with sequences of operations.
Two threads can pass the condition and both write, violating intent.
2. Use TInterlocked for atomic operations
For compare-and-swap patterns:
3. Protect complex invariants with locks
When multiple fields must change together, use TCriticalSection:
Atomic primitives on one field do not guarantee consistency across several fields.
4. Understand memory visibility
Without synchronization, one thread may not immediately observe writes from another due to compiler/CPU reordering. Interlocked operations and lock enter/leave provide memory barriers.
5. Avoid false assumptions by type size
A 64-bit value on 32-bit targets may require extra care for atomicity. Platform differences matter, especially in legacy Delphi targets.
Common Pitfalls
- Assuming primitive types are fully thread-safe for all read-modify-write patterns.
- Using plain increments/decrements on shared variables without
TInterlocked. - Protecting one variable atomically while leaving related invariants unsynchronized.
- Ignoring memory visibility and reordering effects in lock-free code.
- Porting between 32-bit and 64-bit targets without revisiting atomic assumptions.
Summary
Delphi simple types are not inherently thread-safe in concurrent logic. Atomic assignments may exist, but correctness requires synchronization for compound operations and shared invariants. Use TInterlocked for single-variable atomic updates and locks for multi-field consistency. With explicit concurrency control, Delphi code remains correct across architectures and runtime conditions.
A practical way to make this guidance durable is to convert it into a small runbook that includes prerequisites, expected environment versions, and a short verification sequence. Even strong teams lose time when troubleshooting steps live only in memory or chat history. A runbook should explicitly answer three questions: what to check first, what output confirms healthy behavior, and what output indicates a known failure mode. This level of clarity helps both experienced maintainers and newer contributors, and it reduces repeated investigation during incidents.
It is also valuable to create a tiny reproducible fixture for this topic. The fixture can be a minimal script, test case, sample request, or small dataset that demonstrates the correct behavior in isolation. When regressions appear after dependency upgrades, infrastructure changes, or framework migrations, that fixture becomes the fastest way to isolate whether the issue is environmental or logic-related. Keeping a focused fixture in source control gives you a stable benchmark across branches and release cycles.
For long-term reliability, pair documentation with one automated guardrail in CI. The guardrail should be narrow and fast: an import check, schema validation, endpoint contract test, deterministic unit test, or lightweight performance threshold. Avoid broad flaky checks that hide real signals. The goal is early, actionable feedback before code reaches production. If the same category of issue appears repeatedly, promote the manual troubleshooting step into automation so the system catches it first. Over time, this shifts effort from reactive debugging to preventive quality control and keeps the knowledge article relevant in real engineering workflows.

