Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can static arrays be safely accessed from multiple threads?

If each thread is guaranteed to only read/write to a specific subset of the array can multiple threads work on the same (static) array without resorting to critical sections, etc?

EDIT - This is for the specific case of arrays of non-reference-counted types and record/packed-records thereof.

If yes, any caveats?

My gut feeling is yes but my gut can sometimes be an unreliable source of information.

like image 203
J... Avatar asked Apr 16 '12 13:04

J...


2 Answers

Suppose that:

  1. You have a single instance of an array (static or dynamic), and
  2. The elements of the array are pure value types (i.e. contain no references), and
  3. Each thread operates on disjoint sub-arrays, and
  4. Nothing else in the system writes to the array whilst the threads are operating on it.

With these conditions, which I believe are met by your data structure and threading pattern, then all algorithms are thread-safe.

like image 170
David Heffernan Avatar answered Sep 17 '22 12:09

David Heffernan


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.

like image 44
Arnaud Bouchez Avatar answered Sep 19 '22 12:09

Arnaud Bouchez