No, this could not be thread safe, in some situations.
I see at least two reasons.
1. It will depend on the static array content.
If you use some non-reference counted types (like double, integer, bytes, shortstring
), there won't be any issue in most case (at least if data is read/only).
But if you use some reference-counted types (like string, interface
, or a nested dynamic array), you'll have to take care of thread safety.
That is:
TMyType1: array[0..1] of integer; // thread-safe on reading
TMyType2: array[0..1] of string; // may be confusing
Additional note: if your string
is in fact shared among some sub-parts of the static array, you could have the reference count be confused. Unless you explicitly call UniqueString()
for each one (inside a critical section, I suspect). For an array of double
or integer
, you won't have this issue.
2. It will depend on the access concurrency
Read access should be thread safe, even for reference counted type, but concurrent write may be confusing. For a string
, you may have GPF issues in some random cases, especially on a multi-core CPU.
Some safe implementation may be:
- Use critical sections (smaller as possible, to reduce overhead) or other protection structures;
- Use Copy-On-Write or a private per-thread copy of the content, to be sure;
- Latest note (not about safety, but performance): Sharing an array among multiple CPUs may lead into performance penalties due to cache synchronization between CPUs. Performance is sometimes much better when you use separated arrays, ensuring their L1 caching window won't be shared among CPUs.
Be aware that such issues may be a nightmare to debug, on client side: multi-thread concurrency issues may occur randomly, and are very difficult to track. The safer, the better, unless you have explicit and proven performance issues.
Additional note: For your specific case of static array of double, with sub-part of the array accessed by one thread only, it is thread-safe. But there is no absolute rule of thread safeness in all situations, even for a static array. As soon as you use some reference-counted types, or some pointers, you may have random issues.