none
Paralell getting information from array RRS feed

  • Question

  • Hello!

    I have some shell of array and several write and read methods. I want to get it thread-safe and have opportunity to get data from different threads at the same time.

    For example:

    class MyClass
    {
    public void Write1() {...}
    public void Write2() {...}
    public void Read1() {...}
    public void Read2() {...}
    }

    So we could read from different threads at the same time, but lock thread which want to write data.

    And in this way we can't use lock() operation or Monitor, because in this way we can call only one method(read or write) at the same time. What is the best way to do it?


    Thursday, January 30, 2014 3:38 PM

Answers

  • "And in this way we can't use lock() operation or Monitor, because in this way we can call only one method(read or write) at the same time. What is the best way to do it?"

    Well, that fact that only one thread can read/write at the same time is not something wrong so you could certainly use a lock from a correctness point of view.

    From a performance point of view, yes, you would want to allow parallel reads because this increases scalability. The solution is simple, use a reader writer lock - ReaderWriterLock or the newer ReaderWriterLockSlim.

    At least that's the theory. In practice a reader writer lock tends to have higher overhead than a normal lock, if the amount of code which runs inside the lock is small then it's possible that using a reader writer lock will not provide any benefits. Reading/writing from/to an array is a very small amount of code, you should probably stick to using a normal lock.

    Also, do note that just reading/writing from/to an array doesn't necessarily needs any locking. Locking is useful only if there are multiple read/writes involved or if the array type can't be accessed atomically by the CPU your code is running on.

    • Marked as answer by Ruzik Thursday, January 30, 2014 6:02 PM
    Thursday, January 30, 2014 5:41 PM
    Moderator

All replies

  • Hi, do you mean to lock "write" only? if so, you can try this:

    class MyClass
    {
    private object _locker = new object();
    
    public void Write1() {
    lock(_locker)
    {
    ...
    }
    }
    public void Write2() {
    lock(_locker)
    {
    ...
    }
    }
    public void Read1() {...}
    public void Read2() {...}
    }

    Thursday, January 30, 2014 5:23 PM
  • "And in this way we can't use lock() operation or Monitor, because in this way we can call only one method(read or write) at the same time. What is the best way to do it?"

    Well, that fact that only one thread can read/write at the same time is not something wrong so you could certainly use a lock from a correctness point of view.

    From a performance point of view, yes, you would want to allow parallel reads because this increases scalability. The solution is simple, use a reader writer lock - ReaderWriterLock or the newer ReaderWriterLockSlim.

    At least that's the theory. In practice a reader writer lock tends to have higher overhead than a normal lock, if the amount of code which runs inside the lock is small then it's possible that using a reader writer lock will not provide any benefits. Reading/writing from/to an array is a very small amount of code, you should probably stick to using a normal lock.

    Also, do note that just reading/writing from/to an array doesn't necessarily needs any locking. Locking is useful only if there are multiple read/writes involved or if the array type can't be accessed atomically by the CPU your code is running on.

    • Marked as answer by Ruzik Thursday, January 30, 2014 6:02 PM
    Thursday, January 30, 2014 5:41 PM
    Moderator
  • Hi, do you mean to lock "write" only? if so, you can try this:

    class MyClass
    {
    private object _locker = new object();
    
    public void Write1() {
    lock(_locker)
    {
    ...
    }
    }
    public void Write2() {
    lock(_locker)
    {
    ...
    }
    }
    public void Read1() {...}
    public void Read2() {...}
    }

    When I wrote "So we could read from different threads at the same time, but lock thread which want to write data." I mean that
    1) We can't write from several threads at the same time.

    2) We can't write something, if we read data at this time.

     And in this way we can read and write data concurrently.

    Thursday, January 30, 2014 6:01 PM