views:

52

answers:

5

Hi

I am no sure about this ..

Using an int[] array. If in my application, a thread A reads while a thread B writes into the same array element, will everything hold ?

I'd rather not have a synchronized block on reading also - this is used in a web service, so I won't be able to service multiple clients in parallel.

Thanks.

Olivier

A: 

Shared resources that are accessed by multiple threads need to be synchronized.

Since arrays are not thread safe, you need to manage this yourself.

Oded
+1  A: 

No, you need to use a lock block to ensure that no one is trying to read the array while some other process is writing to it. Otherwise, you are likely to have problems.

This is actually quite simple in C#, you can just do this:

// declare an object to use for locking
Object lockObj = new Object();

// declare the array
int[] x = new int[5];

// set the array value (thread safe operation)
public void SetArrayVal(int ndx, int val)
{
    if (ndx < 0 || ndx >= x.Length)
    {
        throw new ArgumentException("ndx was out of range", ndx);
    }    
    lock (lockObj )
    {
        x[ndx] = val;
    }
}

// get the array value (thread safe operation)
public int GetVal(int ndx)
{
    if (ndx < 0 || ndx >= x.Length)
    {
        throw new ArgumentException("ndx was out of range", ndx);
    }    
    lock (lockObj )
    {
        return x[ndx];
    }
}

I wouldn't worry so much about the performance here as to making sure you get the thread saftiness correct, which is critical.

dcp
Thanks for the snippet ! And your comment about performance vs thread safetiness - I was indeed concerned with perf.
@osteinme - You're welcome :). Performance is irrelevant if the data is corrupt, so if you need thread safety, you really have to ensure that the data access is synchronized. I really doubt performance will suffer much unless you are doing millions of operations a second or something.
dcp
A: 

Arrays are not thread safe during reading and writing in threaded environments: http://msdn.microsoft.com/en-us/library/system.array.aspx

You would have to lock the array, a t1.Join(); t2.Join() might work as well although I'm not sure.

Brian
A: 

You'll need to lock access to your array from multiple concurrent threads. The easiest way is to only allow access to your array using getters/setters, and then put some locking code into those.

NeilDurant
+1  A: 

If you're doing more reading than writing, then use ReaderWriterLockSlim to improve performance (though it will probably make your code more verbose.)

If you're worried about blocking your client threads then perhaps you can queue these operations into a threadsafe queue. Honestly though unless you're taking multiple locks for each request, you're unlikely to have to wait long enough on entry to these locks for a human user of your website to notice. This kind of performance impact is more significant when dealing with high throughput with low latency.

Drew Noakes
Thanks, very useful. Unfortunately I am still in 2.0 environment, so will use ReaderWriterLock, but when we upgrade we need to use the slim.