Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing array from multiple threads

Let's say I have two arrays:

int[] array1 = new int[2000000];
int[] array2 = new int[2000000];

I stick some values into the arrays and then want to add the contents of array2 to array1 like so:

for(int i = 0; i < 2000000; ++i) array1[i] += array2[i];

Now, let's say I want to make processing faster on a multi-processor machine, so instead of just doing a loop like above, I create two threads. One of which I have process the first 1000000 elements in the array, the other I have process the last 1000000 elements in the array. My main thread waits for those two threads to notify it that they are finished, and then proceeds to use the values from array1 for all kinds of important stuff. (Note that the two worker threads may not be terminated and may be reused, but the main thread will not resume until they have both notified it to do so.)

So, my question is: How can I be sure that the main thread will see the modifications that the two worker threads made to the array? Can I count on this to happen or do I need to go through some special procedure to make sure the worker threads flush their writes to the array and the main thread discards its cached array values?

like image 386
nonoitall Avatar asked Feb 19 '10 00:02

nonoitall


People also ask

Can multiple threads access the same array?

The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array.

Can multiple threads access the same object?

Objects are not automatically copied. You need to take care that multiple threads are not modifying the same object simultaneously. See this tutorial about concurrency, for example.

Are arrays thread safe Java?

Immutable arrays (declared using let) are thread-safe since it is read-only. But mutable arrays are not thread-safe. Many threads can read a mutable instance of an array simultaneously without an issue but it is unsafe to let one thread modify the array while another is reading it.


4 Answers

If you're lucky and have the option to use .NET 4.0 then you can just write:

Parallel.For(0, 2000000, i => { array1[i] += array2[i]; });

You don't need any explicit locking or synchronization, because:

  • Each task (execution of the for loops body) affects disjoint part of the array
  • Parallel.For waits until all tasks finish before it returns, so there will be an implicit memory barrier.
like image 118
Tomas Petricek Avatar answered Oct 06 '22 02:10

Tomas Petricek


You need a memory barrier to ensure that the worker thread's writes to the array are visible to the main thread in the order you expect.

Whether or not you need an explicit memory barrier depends on how you notify the main thread. Waiting on most synchronization primitives, such as events, provide an implicit barrier, so no change would be required on your part. Polling a global variable does not provide a barrier.

If you need an explicit barrier, use Thread.MemoryBarrier.

like image 20
Michael Avatar answered Oct 06 '22 01:10

Michael


How can I be sure that the main thread will see the modifications that the two worker threads made to the array? Can I count on this to happen or do I need to go through some special procedure to make sure the worker threads flush their writes to the array and the main thread discards its cached array values?

You don't need any special handling here - you'll always be working off the same objects in memory.

Also, since each thread will work on a separate portion of the array, no locking is necessary.

However, if you're only doing a simple addition, the overhead of threading and synchronizing back to the main thread ~might~ outweigh the benefits gained... If you do this, profile to make sure that it's providing a net-gain.

like image 40
Reed Copsey Avatar answered Oct 06 '22 02:10

Reed Copsey


If you break up the index range into non-overlapping ranges as you suggested, then provided the array is created in shared memory (i.e. not by each thread), then no locks are required.

like image 30
Mitch Wheat Avatar answered Oct 06 '22 01:10

Mitch Wheat